import statsmodels.api as sm
import pandas as pd
import os
import itertools
import numpy as np
import pandas as pd
import datetime
# ratio_path= '/home/qastorage/public/yxk/panwei_data/ratio/' 
# price_path=  '/home/qastorage/public/yxk/panwei_data/price/' 
# vol_path=    '/home/qastorage/public/yxk/panwei_data/vol/' 

ratio_path= '/home/sunqy/workspace/jupyter/test/data/Ratio/' 
price_path=  '/home/sunqy/workspace/jupyter/test/data/Price/' 
vol_path=  '/home/sunqy/workspace/jupyter/test/data/Vol/' 
basic_path = '/home/qastorage/public/yxk/hdf_second_factor/'


# ratio_list=[]
# for root, dirs, files in os.walk(ratio_path): 
#     for f in files:
#         ratio_list.append(f)
        
# price_list=[]
# for root, dirs, files in os.walk(price_path): 
#     for f in files:
#         price_list.append(f)

# vol_list=[]
# for root, dirs, files in os.walk(vol_path): 
#     for f in files:
#         vol_list.append(f)  
# allfile=[]
# for priceitem in price_list:     
#     allfile.append(price_path+priceitem)
# for volitem in vol_list:
#     allfile.append(vol_path+volitem)
# for ratioitem in ratio_list:
#     allfile.append(ratio_path+ratioitem)
    
daily_price_list = ['tclose_adj','topen_adj','high_adj','low_adj','vwap']
daily_vol_list = ['volume_raw','amount_raw']
daily_ratio_list = ['change_rate_raw','turnover_rate_raw']

def make_final_add_dict(table='first'):
    add_dict=[]
    if table=='first':     
        for type_item in ['tsmax','tsmin','tshalflifeewm','tsmean','maxsubminrollday','power_num', \
            'tsstd','tsargmax','tsargmin','tskurt','tsskew','tsrank','tsselfregression','tsselfcorr','tstma','tszscore' \
            ,'rankdelta','zscoredelta','tsra','tsragrate','tsrastd','tsradstd','tsrac','tsptra','tsptragrate']  :             
            if type_item=='power_num':      
                for change_num in [2,3,5]:
                    path_dict={}
                    path_dict['mode']=type_item
                    path_dict['a']=change_num
                    add_dict.append(path_dict)
            else:
                for change_num in [2,3,10,20]:
                    path_dict={}
                    path_dict['mode']=type_item
                    path_dict['a']=change_num
                    add_dict.append(path_dict)
    elif table=='final':  
        for type_item in ['tsmax','tsmin','tshalflifeewm','tsmean', 'z_score']  :             
            for change_num in [2,3,5,10,20,30]:
                path_dict={}
                path_dict['mode']=type_item
                path_dict['a']=change_num
                add_dict.append(path_dict)           

    elif table=='mutual':  
        for type_item in ['adivbsubshiftdiv','rankadivbsubshiftdiv','zscoresdivbsubshiftdiv','selfadivsubb',\
                          'rankselfadivsubb','tscov','tscorr','adivbmean','amulbmean']  :             
            for change_num in [3,5,7,10,20]:
                path_dict={}
                path_dict['mode']=type_item
                path_dict['a']=change_num
                add_dict.append(path_dict)   
                

                
    return add_dict    


        

def changeDayData(data):
    df_min_partial = data[np.isin(data.index.time , datetime.time(15, 0))]
    data = (df_min_partial.groupby(df_min_partial.index.date).sum())
    data.index = pd.to_datetime(data.index)
    return data ;




def addfun_str(expressstr):
    express=""
    if 'changeDayData' in  expressstr:
        express+="""
def get_part_min(df_min,endhour,endmin):
    df_min_partial = df_min[np.isin(df_min.index.time , datetime.time(endhour, endmin))]
    return df_min_partial        
def changeDayData(data):
    data = (get_part_min(data,15,0).groupby(get_part_min(data,15,0).index.date).sum())
    data.index = pd.to_datetime(data.index)
    return data
    
"""
    if 'GetDayData' in  expressstr:
        express+="""
def get_part_min(df_min,endhour,endmin):
    df_min_partial = df_min[np.isin(df_min.index.time , datetime.time(endhour, endmin))]
    return df_min_partial
def GetDayData(data):
    data = (get_part_min(data,15,0).groupby(get_part_min(data,15,0).index.date).sum())
    data.index = pd.to_datetime(data.index)
    return data
    
"""
    if 'kmean' in  expressstr:
        express+="""
def kmean(x):
    x_1=x.values.reshape(-1,1)
    a= KMeans(n_clusters=20).fit(x_1).predict(x_1) 
    return pd.Series(a)
            
"""
        
    return express+expressstr


def replace_read_hdf(expressstr):
    dict_tran=pd.read_csv('dict_tran.csv')
    dict_tran.set_index('factor',inplace=True)
    total_line_express=''
    add_data=""
    for line_express in expressstr.split('\n'):

        if "pd.read_hdf" in line_express:
            factorname=line_express.split('/')[-1].split('.')[0]
            index=int(line_express.index("pd.read_hdf"))
            line_expressstr=line_express[:index]+factorname+";\n"
            add_data+=factorname+'='+dict_tran.loc[factorname,'express']+";\n"
        else:
            line_expressstr=line_express+"\n"
        total_line_express+=line_expressstr

    total_line_express=add_data+total_line_express
    return total_line_express


def replace_source_name(expressstr):
    start_date='2015-01-01'
    express=""

#     if 'min_amt' in  expressstr:
#         express+="\nmin_amt=dlib.get_stock_min_data('amt');"
#         express+="\nmin_amt.index = pd.DatetimeIndex(min_amt.index);"
#     if 'min_high' in  expressstr:
#         express+="\min_high=dlib.get_stock_min_data('high');"
#         express+="\nmin_high.index = pd.DatetimeIndex(min_high.index);"
    
    if 'tclose' in  expressstr:
        express+="tclose = dlib.get_stock_daily_data('tclose_adj');\n"
    if 'topen' in  expressstr:
        express+="topen = dlib.get_stock_daily_data('topen_adj');\n"   
    if 'highday' in  expressstr:
        express+="highday = dlib.get_stock_daily_data('high_adj');\n"   
    if 'lowday' in  expressstr:
        express+="lowday = dlib.get_stock_daily_data('low_adj');\n"   
    if 'rets' in  expressstr:
        express+="rets = dlib.get_stock_daily_data('change_rate_raw');\n"   
    if 'volume' in  expressstr:
        express+="volume = dlib.get_stock_daily_data('volume_raw');\n"   
    if 'amount' in  expressstr:
        express+="amount = dlib.get_stock_daily_data('amount_raw');\n"   
    if 'vwap' in  expressstr:
        express+="vwap = dlib.get_stock_daily_data('vwap');\n"   
    if 'turnover' in  expressstr:
        express+="turnover = dlib.get_stock_daily_data('turnover_rate_raw');\n"   
    if 'openmin' in  expressstr:
        express+="openmin = dlib.get_stock_min_data('open_30min_adj');\n"   
    if 'highmin' in  expressstr:
        express+="highmin = dlib.get_stock_min_data('high_30min_adj');\n"    
    if 'lowmin' in  expressstr:
        express+="lowmin = dlib.get_stock_min_data('low_30min_adj');\n"    
    if 'closemin' in  expressstr:
        express+="closemin = dlib.get_stock_min_data('close_30min_adj');\n"    
    if 'tradecountmin' in  expressstr:
        express+="tradecountmin = dlib.get_stock_min_data('tradecount_30min_adj');\n"    
    if 'amtmin' in  expressstr:
        express+="amtmin = dlib.get_stock_min_data('amt_30min_adj');\n"    
    if 'volmin' in  expressstr:
        express+="volmin = dlib.get_stock_min_data('volume_30min_adj');\n"    
    if 'buyamtmin' in  expressstr:
        express+="buyamtmin = dlib.get_stock_min_data('buyamt_30min_adj');\n"    
    if 'buycountmin' in  expressstr:
        express+="buycountmin = dlib.get_stock_min_data('buycount_30min_adj');\n"    
    if 'buyvolmin' in  expressstr:
        express+="buyvolmin = dlib.get_stock_min_data('buyvol_30min_adj');\n"    
    if 'saleamtmin' in  expressstr:
        express+="saleamtmin = dlib.get_stock_min_data('saleamt_30min_adj');\n"    
    if 'salevolmin' in  expressstr:
        express+="salevolmin = dlib.get_stock_min_data('salevol_30min_adj');\n"    
    if 'salecountmin' in  expressstr:
        express+="salecountmin = dlib.get_stock_min_data('salecount_30min_adj');\n"        
    if 'vwapmin' in  expressstr:
        express+="amtmin = dlib.get_stock_min_data('amt_30min_adj');\n"    
        express+="volmin = dlib.get_stock_min_data('volume_30min_adj');\n"    
        express+="vwapmin = (amtmin/volmin).replace([np.inf,-np.inf],np.nan);\n"      
    if 'retmin' in  expressstr:
        express+="closemin = dlib.get_stock_min_data('close_30min_adj');\n"    
        express+="retmin = (np.log(closemin) - np.log(closemin.shift(1))).replace([np.inf,-np.inf],np.nan);\n"    
    if 'priceratio' in  expressstr:
        express+="priceratio = dlib.get('tick','askbid_priceratio');\n"   
    if 'sizeratio' in  expressstr:
        express+="sizeratio = dlib.get('tick','askbid_sizeratio');\n"      
    if 'askprice1' in  expressstr:
        express+="askprice1 = dlib.get('tick','askprice1');\n"   
    if 'askprice2' in  expressstr:
        express+="askprice2 = dlib.get('tick','askprice2');\n"           
    if 'askprice3' in  expressstr:
        express+="askprice3 = dlib.get('tick','askprice3');\n"   
    if 'askprice4' in  expressstr:
        express+="askprice4 = dlib.get('tick','askprice4');\n"      
    if 'askprice5' in  expressstr:
        express+="askprice5 = dlib.get('tick','askprice5');\n"   
    if 'asksize1' in  expressstr:
        express+="asksize1 = dlib.get('tick','asksize1');\n"  
    if 'asksize2' in  expressstr:
        express+="asksize2 = dlib.get('tick','asksize2');\n"   
    if 'asksize3' in  expressstr:
        express+="asksize3 = dlib.get('tick','asksize3');\n"      
    if 'asksize4' in  expressstr:
        express+="asksize4 = dlib.get('tick','asksize4');\n"   
    if 'asksize5' in  expressstr:
        express+="asksize5 = dlib.get('tick','asksize5');\n"                
    if 'bidprice1' in  expressstr:
        express+="bidprice1 = dlib.get('tick','bidprice1');\n"  
    if 'bidprice2' in  expressstr:
        express+="bidprice2 = dlib.get('tick','bidprice2');\n"   
    if 'bidprice3' in  expressstr:
        express+="bidprice3 = dlib.get('tick','bidprice3');\n"      
    if 'bidprice4' in  expressstr:
        express+="bidprice4 = dlib.get('tick','bidprice4');\n"   
    if 'bidprice5' in  expressstr:
        express+="bidprice5 = dlib.get('tick','bidprice5');\n"            
    if 'bidsize1' in  expressstr:
        express+="bidsize1 = dlib.get('tick','bidsize1');\n"  
    if 'bidsize2' in  expressstr:
        express+="bidsize2 = dlib.get('tick','bidsize2');\n"   
    if 'bidsize3' in  expressstr:
        express+="bidsize3 = dlib.get('tick','bidsize3');\n"      
    if 'bidsize4' in  expressstr:
        express+="bidsize4 = dlib.get('tick','bidsize4');\n"   
    if 'bidsize5' in  expressstr:
        express+="bidsize5 = dlib.get('tick','bidsize5');\n"        
    if 'ask_vwap' in  expressstr:
        express+="ask_vwap = dlib.get('tick','ask_vwap');\n"        
    if 'bid_vwap' in  expressstr:
        express+="bid_vwap = dlib.get('tick','bid_vwap');\n"        
    if 'bookprice1' in  expressstr:
        express+="bookprice1 = dlib.get('tick','bookprice1');\n"        
    if 'bookprice2' in  expressstr:
        express+="bookprice2 = dlib.get('tick','bookprice2');\n"        
    if 'bookprice3' in  expressstr:
        express+="bookprice3 = dlib.get('tick','bookprice3');\n"        
    if 'bookprice4' in  expressstr:
        express+="bookprice4 = dlib.get('tick','bookprice4');\n"        
    if 'bookprice5' in  expressstr:
        express+="bookprice5 = dlib.get('tick','bookprice5');\n"     
    if 'askorderflow' in  expressstr:
        express+="askorderflow = dlib.get('tick','askorderflow');\n" 
    if 'bidorderflow' in  expressstr:
        express+="bidorderflow = dlib.get('tick','bidorderflow');\n" 
    if 'STstatus' in  expressstr:
        express+="STstatus=dlib.get_stock_status('st_delisting_status');\n"    
    if 'con_pe_0' in  expressstr:
        express+=f"con_pe_0 = dlib.get_zyyx_data(name='con_forecast_stk',factor_name='con_pe_0').loc['{start_date}':,:];\n"
    if 'cohl' in  expressstr:
        express+="cohl =dlib.get_pv_data('cohl_raw');\n"
    # if 'stock_min' in  expressstr:
    #     express+="stock_min = dlib.get_stock_min_data;\n"           
    if 'NGshenwan1' in  expressstr:
        express+=f"NGshenwan1=dlib.get_zyyx_data('qt_indus_constituents', 'ShenWan1').loc['{start_date}':,:].astype(float);\n"
    if 'NGshenwan2' in  expressstr:
        express+=f"NGshenwan2=dlib.get_zyyx_data('qt_indus_constituents', 'ShenWan2').loc['{start_date}':,:].astype(float);\n" 
    if 'NGshenwan3' in  expressstr:
        express+=f"NGshenwan3=dlib.get_zyyx_data('qt_indus_constituents', 'ShenWan3').loc['{start_date}':,:].astype(float);\n"
    if 'zyyxscore' in  expressstr:
        express+="""
raw = dlib.get_zyyx_data('t_api_stock_score')
raw_A = raw[raw.index_code == '999999']
zyyxscore = raw_A.pivot(index = 'con_date', columns = 'stock_code', values = 'score').astype('float32') ;\n"""
    if 'tcap' in  expressstr:
        express+=f"""
tcap = dlib.get_zyyx_data('qt_stk_daily','tcap').loc['{start_date}':,:].astype(float)
tcap=np.log(tcap) ;\n"""
    if 'KGltcap20' in  expressstr:
        express+=f"""
stock_status = dlib.get_stock_status()
status = stock_status.replace(0,np.nan)
tcap = dlib.get_zyyx_data('qt_stk_daily','tcap').loc['{start_date}':,:].astype(float)
tcap=np.log(tcap)
tcap= (tcap* status).dropna(how='all').fillna(0)
KGltcap20=tcap.apply(lambda x: kmean(x),axis=1)
KGltcap20.columns=tcap.columns ;\n"""
    if 'KGlmcap20' in  expressstr:
        express+=f"""
stock_status = dlib.get_stock_status()
status = stock_status.replace(0,np.nan)
mcap = dlib.get_zyyx_data('qt_stk_daily','mcap').loc['{start_date}':,:].astype(float)
mcap=np.log(mcap)
mcap= (mcap* status).dropna(how='all').fillna(0)
KGlmcap20=mcap.apply(lambda x: kmean(x),axis=1) 
KGlmcap20.columns=mcap.columns ;\n"""
    if 'EGreturn10gourp20' in  expressstr:
        express+=f"""
daily = dlib.get_stock_daily_data
returns = daily(name = 'change_rate_raw')
return10 = returns.rolling(10).sum()
groupcount = 20
return10= return10.loc['{start_date}':,:].copy()
EGreturn10gourp20 = return10.rank(1).apply(lambda x: pd.cut(x, groupcount, labels=range(groupcount)), axis=1).astype(float) ;\n"""
    if 'EGreturn40gourp20' in  expressstr:
        express+=f"""
daily = dlib.get_stock_daily_data
returns = daily(name = 'change_rate_raw')
return10 = returns.rolling(40).sum()
groupcount = 20
return10= return10.loc['{start_date}':,:].copy()
EGreturn40gourp20 = return10.rank(1).apply(lambda x: pd.cut(x, groupcount, labels=range(groupcount)), axis=1).astype(float) ;\n"""    
    if 'EGreturn40gourp40' in  expressstr:
        express+=f"""
daily = dlib.get_stock_daily_data
returns = daily(name = 'change_rate_raw')
return10 = returns.rolling(40).sum()
groupcount = 40
return10= return10.loc['{start_date}':,:].copy().astype(float)
EGreturn40gourp40 = return10.rank(1).apply(lambda x: pd.cut(x, groupcount, labels=range(groupcount)), axis=1).astype(float) ;\n"""    
    if 'EGreturn40gourp100' in  expressstr:
        express+=f"""
daily = dlib.get_stock_daily_data
returns = daily(name = 'change_rate_raw')
return10 = returns.rolling(40).sum()
groupcount = 100
return10= return10.loc['{start_date}':,:].copy().astype(float)
EGreturn40gourp100 = return10.rank(1).apply(lambda x: pd.cut(x, groupcount, labels=range(groupcount)), axis=1).astype(float) ;\n"""    
    if 'EGlmcapgourp20' in  expressstr:
        express+=f"""
daily = dlib.get_stock_daily_data
groupcount = 20
mcap = daily('mcap')
lncap = np.log(mcap).loc['{start_date}':,:].copy()
lncap = lncap.replace([np.inf, -np.inf], np.nan)
EGlmcapgourp20 = lncap.rank(1).apply(lambda x: pd.cut(x, groupcount, labels=range(groupcount)), axis=1).astype(float) ;\n"""
    if 'EGlmcapgourp40' in  expressstr:
        express+=f"""
daily = dlib.get_stock_daily_data
groupcount = 40
mcap = daily('mcap')
lncap = np.log(mcap).loc['{start_date}':,:].copy()
lncap = lncap.replace([np.inf, -np.inf], np.nan)
EGlmcapgourp40 = lncap.rank(1).apply(lambda x: pd.cut(x, groupcount, labels=range(groupcount)), axis=1).astype(float) ;\n"""    
    if 'EGlmcapgourp100' in  expressstr:
        express+=f"""
daily = dlib.get_stock_daily_data
groupcount = 100
mcap = daily('mcap')
lncap = np.log(mcap).loc['{start_date}':,:].copy()
lncap = lncap.replace([np.inf, -np.inf], np.nan)
EGlmcapgourp100 = lncap.rank(1).apply(lambda x: pd.cut(x, groupcount, labels=range(groupcount)), axis=1).astype(float) ;\n"""    
    if 'EGhsigmagourp20' in  expressstr:
        express+=f"""
barra = dlib.get_barra_cne6
groupcount = 20
tmpSigma = barra('h_sigma').loc['{start_date}':,].copy()
EGhsigmagourp20 = tmpSigma.rank(1).apply(lambda x: pd.cut(x, groupcount, labels=range(groupcount)), axis=1).astype(float) ;\n"""  
    if 'EGhsigmagourp40' in  expressstr:
        express+=f"""
barra = dlib.get_barra_cne6
groupcount = 40
tmpSigma = barra('h_sigma').loc['{start_date}':,].copy()
EGhsigmagourp40 = tmpSigma.rank(1).apply(lambda x: pd.cut(x, groupcount, labels=range(groupcount)), axis=1).astype(float) ;\n"""  
    if 'EGhsigmagourp100' in  expressstr:
        express+=f"""
barra = dlib.get_barra_cne6
groupcount = 100
tmpSigma = barra('h_sigma').loc['{start_date}':,].copy()
EGhsigmagourp100 = tmpSigma.rank(1).apply(lambda x: pd.cut(x, groupcount, labels=range(groupcount)), axis=1).astype(float) ;\n"""  
    if 'EGstomgourp20' in  expressstr:
        express+=f"""
barra = dlib.get_barra_cne6
groupcount = 20
tmpstom = barra('stom').loc['{start_date}':,].copy()
EGstomgourp20 = tmpstom.rank(1).apply(lambda x: pd.cut(x, groupcount, labels=range(groupcount)), axis=1).astype(float) ;\n"""    
    if 'EGstomgourp40' in  expressstr:
        express+=f"""
barra = dlib.get_barra_cne6
groupcount = 40
tmpstom = barra('stom').loc['{start_date}':,].copy()
EGstomgourp40 = tmpstom.rank(1).apply(lambda x: pd.cut(x, groupcount, labels=range(groupcount)), axis=1).astype(float) ;\n"""  
    if 'EGstomgourp100' in  expressstr:
        express+=f"""
barra = dlib.get_barra_cne6
groupcount = 100
tmpstom = barra('stom').loc['{start_date}':,].copy()
EGstomgourp100 = tmpstom.rank(1).apply(lambda x: pd.cut(x, groupcount, labels=range(groupcount)), axis=1).astype(float) ;\n"""  
    if 'EGturnover40gourp40' in  expressstr:
        express+=f"""
daily = dlib.get_stock_daily_data
groupcount = 40
turnover = daily(name = 'turnover_rate_raw')
turnover250 = turnover.rolling(40).mean()
turnover250= turnover250.loc['{start_date}':,:].copy()
EGturnover40gourp40 = turnover250.rank(1).apply(lambda x: pd.cut(x, groupcount, labels=range(groupcount)), axis=1).astype(float) ;\n"""
    if 'EGturnover20gourp20' in  expressstr:
        express+=f"""
daily = dlib.get_stock_daily_data
groupcount = 20
turnover = daily(name = 'turnover_rate_raw')
turnover250 = turnover.rolling(20).mean()
turnover250= turnover250.loc['{start_date}':,:].copy()
EGturnover20gourp20 = turnover250.rank(1).apply(lambda x: pd.cut(x, groupcount, labels=range(groupcount)), axis=1).astype(float) ;\n"""  
    if 'EGturnover40gourp100' in  expressstr:
        express+=f"""
daily = dlib.get_stock_daily_data
groupcount = 100
turnover = daily(name = 'turnover_rate_raw')
turnover250 = turnover.rolling(40).mean()
turnover250= turnover250.loc['{start_date}':,:].copy()
EGturnover40gourp100 = turnover250.rank(1).apply(lambda x: pd.cut(x, groupcount, labels=range(groupcount)), axis=1).astype(float) ;\n""" 
        
    if 'EGpegourp20' in  expressstr:
        express+=f"""
con_pe_0 = dlib.get_zyyx_data(name='con_forecast_stk',factor_name='con_pe_0').loc['{start_date}':,:]
groupcount = 20
pe= con_pe_0.loc['{start_date}':,:].copy()
EGpegourp20 = pe.rank(1).apply(lambda x: pd.cut(x, groupcount, labels=range(groupcount)), axis=1).astype(float) ;\n"""     
    if 'EGpegourp40' in  expressstr:
        express+=f"""
con_pe_0 = dlib.get_zyyx_data(name='con_forecast_stk',factor_name='con_pe_0').loc['{start_date}':,:]
groupcount = 40
pe= con_pe_0.loc['{start_date}':,:].copy()
EGpegourp40 = pe.rank(1).apply(lambda x: pd.cut(x, groupcount, labels=range(groupcount)), axis=1).astype(float) ;\n"""  
    if 'EGpegourp100' in  expressstr:
        express+=f"""
con_pe_0 = dlib.get_zyyx_data(name='con_forecast_stk',factor_name='con_pe_0').loc['{start_date}':,:]
groupcount = 100
pe= con_pe_0.loc['{start_date}':,:].copy()
EGpegourp100 = pe.rank(1).apply(lambda x: pd.cut(x, groupcount, labels=range(groupcount)), axis=1).astype(float) ;\n"""  
    
    if 'EGscoregourp20' in  expressstr:
        express+=f"""
score = dlib.get_zyyx_data(name='certainty_score_stk',factor_name='score').loc['{start_date}':,:]
groupcount = 20
score1= score.loc['{start_date}':,:].copy()
EGscoregourp20 = score1.rank(1).apply(lambda x: pd.cut(x, groupcount, labels=range(groupcount)), axis=1).astype(float) ;\n"""
    if 'EGscoregourp40' in  expressstr:
        express+=f"""
score = dlib.get_zyyx_data(name='certainty_score_stk',factor_name='score').loc['{start_date}':,:]
groupcount = 40
score2= score.loc['{start_date}':,:].copy()
EGscoregourp40 = score2.rank(1).apply(lambda x: pd.cut(x, groupcount, labels=range(groupcount)), axis=1).astype(float) ;\n"""

    if 'EG1scoregourp20' in  expressstr:
        express+=f"""
raw = dlib.get_zyyx_data('t_api_stock_score')
raw_A = raw[raw.index_code == '999999']
score = raw_A.pivot(index = 'con_date', columns = 'stock_code', values = 'score').astype('float32')
groupcount = 20
score1= score.loc['{start_date}':,:].copy()
EG1scoregourp20 = score1.rank(1).apply(lambda x: pd.cut(x, groupcount, labels=range(groupcount)), axis=1).astype(float) ;\n"""
    if 'EG1scoregourp40' in  expressstr:
        express+=f"""
raw = dlib.get_zyyx_data('t_api_stock_score')
raw_A = raw[raw.index_code == '999999']
score = raw_A.pivot(index = 'con_date', columns = 'stock_code', values = 'score').astype('float32')
groupcount = 40
score2= score.loc['{start_date}':,:].copy()
EG1scoregourp40 = score2.rank(1).apply(lambda x: pd.cut(x, groupcount, labels=range(groupcount)), axis=1).astype(float) ;\n""" 
    if 'EG1scoregourp100' in  expressstr:
        express+=f"""
raw = dlib.get_zyyx_data('t_api_stock_score')
raw_A = raw[raw.index_code == '999999']
score = raw_A.pivot(index = 'con_date', columns = 'stock_code', values = 'score').astype('float32')
groupcount = 100
score2= score.loc['{start_date}':,:].copy()
EG1scoregourp100 = score2.rank(1).apply(lambda x: pd.cut(x, groupcount, labels=range(groupcount)), axis=1).astype(float) ;\n""" 
    
    if 'lncapgourp1' in  expressstr:
        express+=f"""
shenwan1=dlib.get_zyyx_data('qt_indus_constituents', 'ShenWan1').loc['{start_date}':,:].astype(float)
groupcount = 20
mcap = dlib.get_stock_daily_data('mcap')
lncap = np.log(mcap).loc['{start_date}':,:].copy()
lncap = lncap.replace([np.inf, -np.inf], np.nan)
lncapgourp1 = lncap.rank(1).apply(lambda x: pd.cut(x, groupcount, labels=range(groupcount)), axis=1).astype('float32') ;\n""" 
    if 'returngourp2' in  expressstr:
        express+=f"""
returns = dlib.get_stock_daily_data(name = 'change_rate_raw')
return250 = returns.rolling(40).sum()
groupcount = 40
return250= return250.loc['{start_date}':,:].copy()
returngourp2 = return250.rank(1).apply(lambda x: pd.cut(x, groupcount, labels=range(groupcount)), axis=1).astype('float32') ;\n""" 
    if 'sigmagroup2' in  expressstr:
        express+=f"""
returns = dlib.get_stock_daily_data(name = 'change_rate_raw')
groupcount = 40
return250std = returns.rolling(40).std()
return250std= return250std.loc['{start_date}':,:].copy()
sigmagroup2 = return250std.rank(1).apply(lambda x: pd.cut(x, groupcount, labels=range(groupcount)), axis=1).astype('float32') ;\n""" 
    if 'stomgroup2' in  expressstr:
        express+=f"""
groupcount = 40
turnover = dlib.get_stock_daily_data(name = 'turnover_rate_raw')
turnover250 = turnover.rolling(40).sum()
turnover250= turnover250.loc['{start_date}':,:].copy()
stomgroup2 = turnover250.rank(1).apply(lambda x: pd.cut(x, groupcount, labels=range(groupcount)), axis=1).astype('float32') ;\n""" 
#     if 'get_part_min' in  expressstr:
#         express+="""
# def get_part_min(df_min,endhour,endmin):
#     df_min_partial = df_min[np.isin(df_min.index.time , datetime.time(endhour, endmin))]
#     return df_min_partial ;\n""" 
#     if 'changeDayData' in  expressstr:
#         express+="""
# def get_part_min(df_min,endhour,endmin):
#     df_min_partial = df_min[np.isin(df_min.index.time , datetime.time(endhour, endmin))]
#     return df_min_partial
        
# def changeDayData(data):
#     data = (get_part_min(data,15,0).groupby(get_part_min(data,15,0).index.date).sum())
#     data.index = pd.to_datetime(data.index)
#     return data ;\n""" 
#     if 'GetDayData' in  expressstr:
#         express+="""
# def get_part_min(df_min,endhour,endmin):
#     df_min_partial = df_min[np.isin(df_min.index.time , datetime.time(endhour, endmin))]
#     return df_min_partial
    
# def GetDayData(data):
#     data = (get_part_min(data,15,0).groupby(get_part_min(data,15,0).index.date).sum())
#     data.index = pd.to_datetime(data.index)
#     return data ;\n""" 
#     if 'min30' in  expressstr:
#         express+="""
# def min30(name):
#     factorname = '%s_30min_adj'%name
#     return dlib.get_stock_min_data(factorname) ;\n"""
    
    return express+expressstr


    
        
#         min_amt=dlib.get_stock_min_data(name = 'amt')
# min_high=dlib.get_stock_min_data(name = 'high')
# min_low=dlib.get_stock_min_data(name = 'low')
# min_amt.index = pd.DatetimeIndex(min_amt.index)
# min_high.index = pd.DatetimeIndex(min_high.index)
# min_low.index = pd.DatetimeIndex(min_low.index)
       
   
    
def change_factor_signal(change_num):
  
    if change_num['mode'] == 'getdata':
        express="\ndata1="+str(change_num['a'])+";"
    # elif change_num['mode'] == 'daily':
    #     express="\ndata1=dlib.get_stock_daily_data('"+str(change_num['a'])+"');"   
    # elif change_num['mode'] == 'zyyx':
    #     express="\ndata1=dlib.get_zyyx_data('"+str(change_num['a'])+"');" 
    # elif change_num['mode'] == 'min30':
    #     express="\ndata1=dlib.get_stock_min_data('"+str(change_num['a'])+"_30min_adj');"
    elif change_num['mode'] == 'gethdfdata':
        express="\ndata1=pd.read_hdf('"+str(change_num['a'])+"');"
    elif change_num['mode'] == 'gethdfdata_3':
        express="\ndata1=pd.read_hdf('"+str(change_num['a'][0])+"')"
        express+="\ndata2=pd.read_hdf('"+str(change_num['a'][1])+"')"
        express+="\ndata3=pd.read_hdf('"+str(change_num['a'][2])+"')"
    elif change_num['mode'] == 'tsmax':
        express="\nreturndata=alphalib.ts_max(data1,"+str(change_num['a'])+",1);"
        # returndata=alphalib.ts_max(data1,change_num['a'],1)
    elif change_num['mode'] == 'tsmin':
        express="\nreturndata=alphalib.ts_min(data1,"+str(change_num['a'])+",1);"
    elif change_num['mode'] == 'delta':
        express="\nreturndata=alphalib.delta(data1,"+str(change_num['a'])+");"
    elif change_num['mode'] == 'decay_linear':
        express="\nreturndata=alphalib.decay_linear(data1,"+str(change_num['a'])+");"
        # returndata=alphalib.ts_min(data1,change_num['a'],1)    
    elif change_num['mode'] == 'tshalflifeewm':
        express="\nreturndata=alphalib.ts_halflife_ewm(data1,120,"+str(change_num['a'])+");"
        # returndata=alphalib.ts_halflife_ewm(data1,120,change_num['a'])
    elif change_num['mode'] == 'tsmean':
        express="\nreturndata=alphalib.ts_mean(data1,"+str(change_num['a'])+",1);"
        # returndata=alphalib.ts_mean(data1,change_num['a'],1)
    elif change_num['mode'] == 'maxsubminrollday':
        express="max = data1.rolling("+str(change_num['a'])+", 1).max();\
        \nmin = data1.rolling("+str(change_num['a'])+",1).min();\
        \nreturndata = max - min;"
        # express="\nreturndata=alphalib.ts_mean(data1,"+str(change_num['a'])+",1)"
        # max = data1.rolling(change_num['a'], center=False, 1).max()
        # min = data1.rolling(change_num['a'], center=False, 1).min()
    
    elif change_num['mode'] == 'tssum':
        express="\nreturndata=alphalib.ts_sum(data1,"+str(change_num['a'])+");"  
    
    elif change_num['mode'] == 'z_score':
        express="\nreturndata=alphalib.z_score(data1,outliner='mad');"    
    elif change_num['mode'] == 'negmaxadivbrollday':
        express="\nreturndata = -1* (data1.rolling("+str(change_num['a'])+").max()/data2.rolling("+str(change_num['a'])+").max()).replace([np.inf,-np.inf],np.nan);"
    elif change_num['mode'] == 'negmeanadivbrollday':
        express="\nreturndata = -1* (data1.rolling("+str(change_num['a'])+").mean()/data2.rolling("+str(change_num['a'])+").mean()).replace([np.inf,-np.inf],np.nan);"    
    elif change_num['mode'] == 'power_num':
        express="\nreturndata = np.abs(np.power(data1,"+str(change_num['a'])+"))* np.sign(data1);"
        # returndata = np.abs(np.power(data1,change_num['a']))* np.sign(data1)
    elif change_num['mode'] == 'reverse':
        express="\nreturndata =  -1 * data1;"
        # returndata =  -1 * data1   
    elif change_num['mode'] == 'diff':
        express="\nreturndata=data1-data1.shift("+str(change_num['a'])+");"
    elif change_num['mode'] == 'growth':
        express="\nreturndata=data1/data1.shift("+str(change_num['a'])+").replace(0,np.nan);"
        # returndata=data1-data1.shift(change_num['a'])
    elif change_num['mode'] == 'tsstd':
        express="\nreturndata=alphalib.ts_std(data1,"+str(change_num['a'])+",1);"
        # returndata=alphalib.ts_std(data1,change_num['a'],1)
    elif change_num['mode'] == 'tsargmax':
        express="\nreturndata=alphalib.ts_argmax(data1,"+str(change_num['a'])+",1);"
        # returndata=alphalib.ts_argmax(data1,change_num['a'],1)                   
    elif change_num['mode'] == 'tsargmin':
        express="\nreturndata=alphalib.ts_argmin(data1,"+str(change_num['a'])+",1);"
        # returndata=alphalib.ts_argmin(data1,change_num['a'],1)    
    elif change_num['mode'] == 'product':
        express="\nreturndata=product(data1,"+str(change_num['a'])+",1);"
        # returndata=product(data1,change_num['a'],1)    
    elif change_num['mode'] == 'tskurt':
        express="\nreturndata=alphalib.ts_kurt(data1,"+str(change_num['a'])+",1);"
        # returndata=alphalib.ts_kurt(data1,change_num['a'],1)   
    elif change_num['mode'] == 'tsskew':
        express="\nreturndata=alphalib.ts_skew(data1,"+str(change_num['a'])+",1);"
        # returndata=alphalib.ts_skew(data1,change_num['a'],1)                 
    elif change_num['mode'] == 'tsrank':
        express="\nreturndata=alphalib.ts_rank(data1,"+str(change_num['a'])+",1);"
        # returndata=alphalib.ts_rank(data1,change_num['a'],1)    
    elif change_num['mode'] == 'tsselfregression':
        express="\nreturndata=alphalib.ts_self_regression(data1,"+str(change_num['a'])+",1);"
        # returndata=alphalib.ts_self_regression(data1,change_num['a'],1)    
    elif change_num['mode'] == 'tsselfcorr':
        express="\nreturndata=alphalib.ts_self_corr(data1,"+str(change_num['a'])+",1);"
        # returndata=alphalib.ts_self_corr(data1,change_num['a'],1)
    elif change_num['mode'] == 'tstma':
        express="\nreturndata=alphalib.ts_tma(data1,"+str(change_num['a'])+");"
        # returndata=alphalib.ts_tma(data1,change_num['a'])   
    elif change_num['mode'] == 'tszscore':
        express="\nreturndata=alphalib.ts_z_score(data1,"+str(change_num['a'])+",1);"
        # returndata=alphalib.ts_z_score(data1,change_num['a'],1)             
    elif change_num['mode'] == 'rankdelta':
        express="\nreturndata=alphalib.rank(data1) -alphalib.rank(data1.shift("+str(change_num['a'])+"));  "
        # returndata=alphalib.rank(data1) -alphalib.rank(data1.shift(change_num['a']))    
    elif change_num['mode'] == 'zscoredelta':
        express="\nreturndata=alphalib.z_score(data1) -alphalib.z_score(data1.shift("+str(change_num['a'])+")); "
        # returndata=alphalib.z_score(data1) -alphalib.z_score(data1.shift(change_num['a']))                                                      
    elif change_num['mode'] == 'tsra':
        express="\nreturndata=data1- alphalib.ts_mean(data1,"+str(change_num['a'])+",1) ; "
        # returndata=data1- alphalib.ts_mean(data1,change_num['a'],1)           
    elif change_num['mode'] == 'tsragrate': 
        express="\nreturndata=((data1/alphalib.ts_mean(data1,"+str(change_num['a'])+",1)).replace([np.inf,-np.inf],np.nan)-1);  "
        # returndata=(data1/alphalib.ts_mean(data1,change_num['a'],1)-1)       
    elif change_num['mode'] == 'tsrastd': 
        express="\nreturndata= (data1 - alphalib.ts_mean(data1,"+str(change_num['a'])+",1)) * alphalib.ts_std(data1,"+str(change_num['a'])+",1);  "
        # returndata= (data1 - alphalib.ts_mean(data1,change_num['a'],1)) * alphalib.ts_std(data1,change_num['a'],1)                                                                   
    elif change_num['mode'] == 'tsradstd':
        express="\nreturndata= ((data1 - alphalib.ts_mean(data1,"+str(change_num['a'])+",1)) / alphalib.ts_std(data1,"+str(change_num['a'])+",1)).replace([np.inf,-np.inf],np.nan) ;  "
        # returndata= (data1 - alphalib.ts_mean(data1,change_num['a'],1)) / alphalib.ts_std(data1,change_num['a'],1)  
    elif change_num['mode'] == 'tsrac':
        express="_cohl = cohl.reindex_like(data1);\
        \nreturndata=((data1 - alphalib.ts_mean(data1,"+str(change_num['a'])+",1)/_cohl)).replace([np.inf,-np.inf],np.nan);"
        # _cohl = cohl.reindex_like(data1)
        # returndata=(data1 - alphalib.ts_mean(data1,change_num['a'],1)/_cohl)           
    elif change_num['mode'] == 'tsraret5':
        express="_cohl = cohl.reindex_like(data1);\
        \nreturndata=(data1 - alphalib.ts_mean(data1,"+str(change_num['a'])+",1)/tommorow_ret5);"
        # _cohl = cohl.reindex_like(data1)
        # returndata=(data1 - alphalib.ts_mean(data1,change_num['a'],1)/tommorow_ret5)    
    elif change_num['mode'] == 'tsptra':     
        express="_cohl = cohl.reindex_like(data1);\
        \n_fra = (data1 - alphalib.ts_mean(data1,"+str(change_num['a'])+",1));\
        \n_pra = (_cohl -  alphalib.ts_mean(_cohl,"+str(change_num['a'])+",1));\
        \nreturndata= _fra*(1. - np.sqrt(np.abs(_fra*_pra))*np.sign(_fra)*np.sign(_pra) ) ;"
        # _cohl = cohl.reindex_like(data1)
        # _fra = (data1 - alphalib.ts_mean(data1,change_num['a'],1))
        # _pra = (_cohl -  alphalib.ts_mean(_cohl,change_num['a'],1))
        # returndata= _fra*(1. - np.sqrt(np.abs(_fra*_pra))*np.sign(_fra)*np.sign(_pra) )    
    elif change_num['mode'] == 'tsptragrate':  
        express="_cohl = cohl.reindex_like(data1);\
        \n_fra = (data1/alphalib.ts_mean(data1,"+str(change_num['a'])+",1) -1.).replace([np.inf,-np.inf],np.nan);\
        \n_pra = (_cohl/alphalib.ts_mean(_cohl,"+str(change_num['a'])+",1) -1.).replace([np.inf,-np.inf],np.nan);\
        \nreturndata= _fra*(1. - np.sqrt(np.abs(_fra*_pra))*np.sign(_fra)*np.sign(_pra) );"
        # _fra = (data1/alphalib.ts_mean(data1,change_num['a'],1) -1.).replace([np.inf,-np.inf],np.nan)
        # _pra = (_cohl/alphalib.ts_mean(_cohl,change_num['a'],1) -1.).replace([np.inf,-np.inf],np.nan)
        # returndata= _fra*(1. - np.sqrt(np.abs(_fra*_pra))*np.sign(_fra)*np.sign(_pra) ) 
    elif change_num['mode'] == 'tsptragrate':  
        express="_cohl = cohl.reindex_like(data1);\
        \n_fra = (data1/alphalib.ts_mean(data1,"+str(change_num['a'])+",1) -1.).replace([np.inf,-np.inf],np.nan);\
        \n_pra = (_cohl/alphalib.ts_mean(_cohl,"+str(change_num['a'])+",1) -1.).replace([np.inf,-np.inf],np.nan);\
        \nreturndata= _fra*(1. - np.sqrt(np.abs(_fra*_pra))*np.sign(_fra)*np.sign(_pra) );"
        # _cohl = dlib.get_pv_data('cohl_raw').reindex_like(data1)
        # _fra = (data1/alphalib.ts_mean(data1,change_num['a'],1) -1.).replace([np.inf,-np.inf],np.nan)
        # _pra = (_cohl/alphalib.ts_mean(_cohl,change_num['a'],1) -1.).replace([np.inf,-np.inf],np.nan)
        # returndata= _fra*(1. - np.sqrt(np.abs(_fra*_pra))*np.sign(_fra)*np.sign(_pra) )
    elif change_num['mode'] == 'adivbmean':
        express="\nreturndata=alphalib.ts_mean((data1.replace(0,np.nan)/data2).replace([np.inf,-np.inf],np.nan),"+str(change_num['a'])+",1);"
        # returndata=alphalib.ts_mean((data1.replace(0,np.nan)/data2).replace([np.inf,-np.inf],np.nan),change_num['a'],1)

    elif change_num['mode'] == 'amulbmean': 
        express="\nreturndata=alphalib.ts_mean((data1*data2).replace([np.inf,-np.inf],np.nan),"+str(change_num['a'])+",1);"
        
        # returndata=alphalib.ts_mean((data1*data2).replace([np.inf,-np.inf],np.nan),change_num['a'],1)     
    elif change_num['mode'] == 'adivbsubshiftdiv': 
        express="\nreturndata=(data1/data2).replace([np.inf,-np.inf],np.nan)\
        -(data1.shift("+str(change_num['a'])+")/data2.shift("+str(change_num['a'])+")).replace([np.inf,-np.inf],np.nan);"
   
    elif change_num['mode'] == 'amulbsum': 
        express="\nreturndata=alphalib.ts_sum((data1*data2).replace([np.inf,-np.inf],np.nan),"+str(change_num['a'])+",1);"  
    elif change_num['mode'] == 'asubbargmax':
        express="\nreturndata=alphalib.ts_argmax((data1-data2),"+str(change_num['a'])+",1);"
    elif change_num['mode'] == 'amulbcumsum':
        express="\nreturndata=(data1*data2).cumsum()"
        
    elif change_num['mode'] == 'meanasubb':
        express="\nreturndata=alphalib.ts_mean(data1.replace([np.inf,-np.inf],np.nan),"+str(change_num['a'][0])+",1)-alphalib.ts_mean(data1.replace([np.inf,-np.inf],np.nan),"+str(change_num['a'][1])+",1);"
    elif change_num['mode'] == 'halflifeasubb':
        express="\nreturndata=alphalib.ts_halflife_ewm(data1,120,"+str(change_num['a'][0])+")-alphalib.ts_halflife_ewm(data1,120,"+str(change_num['a'][1])+");"
    elif change_num['mode'] == 'selfsubhalflife':
        express="\nreturndata=data1-alphalib.ts_halflife_ewm(data1,120,"+str(change_num['a'])+") ;"
    elif change_num['mode'] == 'rankadivbsubshiftdiv': 
        express="\nreturndata=alphalib.rank(data1/data2.replace(0,np.nan))-alphalib.rank(data1.shift("+str(change_num['a'])+")/data2.shift("+str(change_num['a'])+").replace(0,np.nan));"
    elif change_num['mode'] == 'zscoresdivbsubshiftdiv': 
        express="\nreturndata=alphalib.z_score(data1/data2.replace(0,np.nan))-alphalib.z_score(data1.shift("+str(change_num['a'])+")/data2.shift("+str(change_num['a'])+").replace(0,np.nan)) ;"
    elif change_num['mode'] == 'selfadivsubb': 
        express="\nreturndata=data1/data1.shift("+str(change_num['a'])+").replace(0,np.nan)-data2/data2.shift("+str(change_num['a'])+").replace(0,np.nan) ;"
    elif change_num['mode'] == 'rankselfadivsubb': 
        express="\nreturndata=alphalib.rank(data1/data1.shift("+str(change_num['a'])+").replace(0,np.nan))-alphalib.rank(data2/data2.shift("+str(change_num['a'])+").replace(0,np.nan));"
        
    elif change_num['mode'] == 'negselfadivrollingmean': 
        express="\nreturndata=-1*(data1/data1.rolling("+str(change_num['a'])+").mean()-1);"
        # returndata=rank(data1/data1.shift(change_num['a']))-rank(data2/data2.shift(change_num['a']))
    elif change_num['mode'] == 'tscov': 
        express="\nreturndata=alphalib.ts_cov(data1, data2,"+str(change_num['a'])+", 1);"
        # returndata=alphalib.ts_cov(data1, data2,change_num['a'], 1)
    elif change_num['mode'] == 'tscorr': 
        express="\nreturndata=alphalib.ts_corr(data1, data2,"+str(change_num['a'])+", 1)  ;"
        express+="\nreturndata[returndata>1]=1"
        express+="\nreturndata[returndata<-1]=-1"
        # returndata=alphalib.ts_corr(data1, data2,change_num['a'], 1)        
        
    elif change_num['mode'] == 'changeDayDatamax':     
        express="\nreturndata = data1.groupby(data1.index.date).max();"
        express+="\nreturndata.index= pd.DatetimeIndex(returndata.index) ;"
    elif change_num['mode'] == 'changeDayDatamin':     
        express="\nreturndata = data1.groupby(data1.index.date).min();"
        express+="\nreturndata.index= pd.DatetimeIndex(returndata.index) ;"
    elif change_num['mode'] == 'changeDayDatafirst':     
        express="\nreturndata = data1.groupby(data1.index.date).first();"
        express+="\nreturndata.index= pd.DatetimeIndex(returndata.index) ;"
    elif change_num['mode'] == 'changeDayDatalast':     
        express="\nreturndata = data1.groupby(data1.index.date).last();"
        express+="\nreturndata.index= pd.DatetimeIndex(returndata.index) ;"
    elif change_num['mode'] == 'changeDayDatadiff':     
        express="\nreturndata = data1.groupby(data1.index.date).last()-data1.groupby(data1.index.date).first();"
        express+="\nreturndata.index= pd.DatetimeIndex(returndata.index) ;"
    elif change_num['mode'] == 'changeDayDatamaxsubmean':     
        express="\nreturndata = data1.groupby(data1.index.date).max()-data1.groupby(data1.index.date).mean();"
        express+="\nreturndata.index= pd.DatetimeIndex(returndata.index) ;"    
    elif change_num['mode'] == 'changeDayDataminsubmean':     
        express="\nreturndata = data1.groupby(data1.index.date).min()-data1.groupby(data1.index.date).mean();"
        express+="\nreturndata.index= pd.DatetimeIndex(returndata.index) ;"        
    elif change_num['mode'] == 'changeDayDatameansubmedian':     
        express="\nreturndata = data1.groupby(data1.index.date).mean()-data1.groupby(data1.index.date).median();"
        express+="\nreturndata.index= pd.DatetimeIndex(returndata.index) ;"        
    elif change_num['mode'] == 'changeDayDatamedian':     
        express="\nreturndata = data1.groupby(data1.index.date).median();"
        express+="\nreturndata.index= pd.DatetimeIndex(returndata.index) ;"    
    elif change_num['mode'] == 'changeDayDataskew':     
        express="\nreturndata = data1.groupby(data1.index.date).skew();"
        express+="\nreturndata.index= pd.DatetimeIndex(returndata.index) ;"      
    elif change_num['mode'] == 'changeDayDatamean':     
        express="\nreturndata = data1.groupby(data1.index.date).mean();"
        express+="\nreturndata.index= pd.DatetimeIndex(returndata.index) ;"          
    elif change_num['mode'] == 'changeDayDatastd':     
        express="\nreturndata = data1.groupby(data1.index.date).std();"
        express+="\nreturndata.index= pd.DatetimeIndex(returndata.index) ;"  
    elif change_num['mode'] == 'changeDayDatacount':     
        express="\nreturndata = data1.groupby(data1.index.date).count();"
        express+="\nreturndata.index= pd.DatetimeIndex(returndata.index) ;"  
    elif change_num['mode'] == 'changeDayDatakurt':     
        express="\nreturndata = data1.groupby(data1.index.date).apply(pd.DataFrame.kurt);"
        express+="\nreturndata.index= pd.DatetimeIndex(returndata.index) ;" 
    elif change_num['mode'] == 'changeDayDatacountovermedian':  
        express="\nreturndata = (data1>data1.groupby(data1.index.date).transform('median')).groupby(data1.index.date).count();"
        express+="\nreturndata.index= pd.DatetimeIndex(returndata.index) ;" 
    elif change_num['mode'] == 'growthrate':      
        express="\nreturndata=(data1-data1.shift("+str(change_num['a'])+"))/data1.shift("+str(change_num['a'])+").replace(0,np.nan)" 
    elif change_num['mode'] == 'convexity':
        express="\nreturndata=(2*data1-data2-data3)/(data2+data3).replace(0,np.nan) ;"
    elif change_num['mode'] == 'negadivbmulN':
        express="\nreturndata=-1*data1/data2.replace(0,np.nan) ;"
    elif change_num['mode'] == 'voladjust':
        express="\ndata2[data1-data1.shift(1)<=0]=-1*data2"
        express+="\nreturndata=data2 ;"
    elif change_num['mode'] == 'rollingmean':
        express="\nreturndata=data1.rolling("+str(change_num['a'])+").mean() ;"
    elif change_num['mode'] == 'rollingmax':
        express="\nreturndata=data1.rolling("+str(change_num['a'])+").max() ;"  
    elif change_num['mode'] == 'rollingmin':
        express="\nreturndata=data1.rolling("+str(change_num['a'])+").min() ;"
    elif change_num['mode'] == 'rollingstd':
        express="\nreturndata=data1.rolling("+str(change_num['a'])+").std() ;"
    elif change_num['mode'] == 'rollingkurt':
        express="\nreturndata=data1.rolling("+str(change_num['a'])+").apply(pd.DataFrame.kurt) ;"
    elif change_num['mode'] == 'selfsubdivmean':
        express="\nreturndata=-1*(data1-alphalib.ts_mean(data1,"+str(change_num['a'])+",1))/alphalib.ts_mean(data1,"+str(change_num['a'])+",1).replace(0,np.nan) ;"   
    elif change_num['mode'] == 'emvfunc':
        express="\ndata1=alphalib.ts_mean(data1,"+str(change_num['a'])+",1)/data1.replace(0,np.nan)"
        express+="\nreturndata= 100*(data2-data2.shift(1))/data2.replace(0,np.nan)"
        express+="\nreturndata= data1*data2*returndata/alphalib.ts_mean(data2,"+str(change_num['a'])+",1).replace(0,np.nan) ;"
    elif change_num['mode'] == 'asubbstdb':
        express="\nreturndata = data2+2*alphalib.ts_std(data1,"+str(change_num['a'])+",1)-data1 ;"
    elif change_num['mode'] == 'rsvfunc':     
        express="\na=alphalib.ts_max(data1,"+str(change_num['a'])+",1)-data2"
        express+="\nb=alphalib.ts_max(data1,"+str(change_num['a'])+",1)-alphalib.ts_min(data3,"+str(change_num['a'])+",1)"
        express+="\nreturndata =100*a/b.replace(0,np.nan) ;"
    elif change_num['mode'] == 'asubrollingmean': 
        express="\nreturndata=data1-data1.rolling("+str(change_num['a'])+").mean() ;"
    elif change_num['mode'] == 'normalize':
        express="\nreturndata=(data1-data1.rolling("+str(change_num['a'])+").mean()) / (data1.rolling("+str(change_num['a'])+").std()).replace(0,np.nan) ;"
    elif change_num['mode'] == 'setST':
        express="\nreturndata=STstatus[STstatus==0]+data1 ;"
    elif change_num['mode'] == 'weighted':
        express="\nweighted=data1.replace(np.nan,0)/alphalib.ts_sum(data1,"+str(change_num['a'])+",1)"
        express+="\nreturndata=data2*weighted ;"  
    elif change_num['mode'] == 'rankcorr':
        express="\nreturndata= alphalib.ts_corr(data1.rank(axis=1,pct=True),data2.rank(axis=1,pct=True),"+str(change_num['a'])+",1) ;" 
        express+="\nreturndata[returndata>1]=1"
        express+="\nreturndata[returndata<-1]=-1"
    elif change_num['mode'] == 'rankasubbmean':
        express="\nreturndata= (data1-alphalib.ts_mean(data2,"+str(change_num['a'])+",1)).rank(axis=1,pct=True) ;"
    elif change_num['mode'] == 'rankasubb':
        express="\nreturndata= (data1-data2).rank(axis=1,pct=True) ;"
    elif change_num['mode'] == 'keep':
        express="\nreturndata=data1 ;"  
    elif change_num['mode'] == 'aplusb':
        express="\nreturndata= data1+data2 ;"    
    elif change_num['mode'] == 'asubb':
        express="\nreturndata= data1-data2 ;"  
    elif change_num['mode'] == 'amulb':
        express="\nreturndata= data1*data2 ;"
    elif change_num['mode'] == 'adivb':
        express="\nreturndata= data1/data2.replace(0,np.nan) ;"    
    elif change_num['mode'] == 'avg2':
        express="\nreturndata= 0.5*(data1+data2) ;"
    elif change_num['mode'] == 'avg3':
        express="\nreturndata= (data1+data2+data3)/3 ;"
    elif change_num['mode'] == 'avg4':
        express="\nreturndata= (data1+data2+data3+data4)/4 ;"
    elif change_num['mode'] == 'avg5':
        express="\nreturndata= (data1+data2+data3+data4+data5)/5 ;"
    elif change_num['mode'] == 'changeDayData':
        express="\nreturndata= changeDayData(data1) ;"
    else: 
        print("!!"*30,change_num)
        return False
    
    
#     express+="\nreturndata=returndata.replace([np.inf,-np.inf],np.nan);\n"
    return express   

def tclosedivvwap():
    step0=[]
    for sorce in ['topen']:     
        path_dict={}
        path_dict['mode']='getdata'
        path_dict['a']=sorce
        step0.append(path_dict) 
    step1=[]
    for fun1 in ['decay_linear','tsmin','tsmax','tsmean','tshalflifeewm']:
        for fun1p1 in [3,5,7,10,20]:
            path_dict={}
            path_dict['mode']=fun1
            path_dict['a']=fun1p1
            step1.append(path_dict) 
    step2=[]       
    for fun1 in ['changeDayDatamin','changeDayDatafirst','changeDayDatalast','changeDayDatadiff','changeDayDatamaxsubmean']:
        path_dict={}
        path_dict['mode']=fun1
        step2.append(path_dict)         
    step3=[]
    for fun1p1 in [3,5,7,10,20]:
        path_dict={}
        path_dict['mode']='adivbmean'
        path_dict['a']=fun1p1
        step3.append(path_dict) 

    total_task=[]
    change_num = list(itertools.product(step0,step1, step2,step3))   
#         print('len(change_num)',len(change_num))
    for change_item in change_num:
        express=''
        for i in range(len(change_item)):
            result_express =change_factor_signal(change_item[i]) 
            if not result_express:
                print('error')
                break;
            if i==3:
                result_express=str(result_express).replace('data1','tclose').replace('data2','returndata')
            elif i in [2]:
                result_express=str(result_express).replace('data1','returndata')  
            express+=result_express
        total_task.append(express)
    return total_task

def pv_vpt():
    step0=[]
    for volitem in vol_list:     
        path_dict={}
        path_dict['mode']='gethdfdata'
        path_dict['a']=vol_path+volitem
        step0.append(path_dict) 
    step1=[]     
    for priceitem in price_list:     
        path_dict={}
        path_dict['mode']='gethdfdata'
        path_dict['a']=price_path+priceitem
        step1.append(path_dict) 

    step2=[]
    for fun1 in ['growthrate']:
        for fun1p1 in [1]:
            path_dict={}
            path_dict['mode']=fun1
            path_dict['a']=fun1p1
            step2.append(path_dict) 

    step3=[]
    for fun1 in ['amulbsum']:
        for fun1p1 in [5,15]:
            path_dict={}
            path_dict['mode']=fun1
            path_dict['a']=fun1p1
            step3.append(path_dict)    

    step4=[]    
    for fun1 in ['decay_linear','tsmin','tsmax','tsmean']:
        for fun1p1 in [5]:
            path_dict={}
            path_dict['mode']=fun1
            path_dict['a']=fun1p1
            step4.append(path_dict) 
    total_task=[]
    change_num = list(itertools.product(step0,step1, step2,step3,step4))   
    print('len(change_num)',len(change_num))

    for change_item in change_num:
        express=''
        for i in range(len(change_item)):
            result_express =change_factor_signal(change_item[i]) 
    #         print(result_express)
            if not result_express:
                print('error')
                break;

            elif i in [1,2]:
                result_express=str(result_express).replace('data1','data2')
            elif i in [3]:
                result_express=str(result_express).replace('data2','returndata')     
            elif i in [4]:
                result_express=str(result_express).replace('data1','returndata')        
            express+=result_express
        total_task.append(express)
    return total_task


def pv_cho():
    step0=[]
    pos=np.random.randint(len(price_list),size=600).reshape(200,3)
    for i in pos:     
        path_dict={}
        path_dict['mode']='gethdfdata_3'
        path_dict['a']=[price_path+price_list[i[0]],price_path+price_list[i[1]],price_path+price_list[i[2]]]
        step0.append(path_dict) 


    step1=[]
    for volitem in vol_list:     
        path_dict={}
        path_dict['mode']='gethdfdata'
        path_dict['a']=vol_path+volitem
        step1.append(path_dict) 

    step2=[]
    # for fun1 in ['tsstd','tsargmax','tsargmin','product','tskurt','tsskew','tsrank','tsselfregression','tsselfcorr','tstma','tszscore']:
    for fun1 in ['convexity']:
        path_dict={}
        path_dict['mode']=fun1
        step2.append(path_dict) 

    step3=[]
    # for fun1 in ['tsstd','tsargmax','tsargmin','product','tskurt','tsskew','tsrank','tsselfregression','tsselfcorr','tstma','tszscore']:
    for fun1 in ['amulbcumsum']:
        path_dict={}
        path_dict['mode']=fun1
        step3.append(path_dict) 

    step4=[]
    # for fun1 in ['tsstd','tsargmax','tsargmin','product','tskurt','tsskew','tsrank','tsselfregression','tsselfcorr','tstma','tszscore']:
    for fun1 in ['meanasubb']:
        for fun1p1 in [(5,10)]:
            path_dict={}
            path_dict['mode']=fun1
            path_dict['a']=fun1p1
            step4.append(path_dict)  

    step5=[]    
    for fun1 in ['decay_linear','tsmin']:
        for fun1p1 in [5]:
            path_dict={}
            path_dict['mode']=fun1
            path_dict['a']=fun1p1
            step5.append(path_dict)  

    total_task=[]
    change_num = list(itertools.product(step0,step1,step2,step3,step4,step5))   
    print('len(change_num)',len(change_num))
    # print(change_num[0])

    for change_item in change_num:
        express=''
        for i in range(len(change_item)):
            result_express =change_factor_signal(change_item[i]) 
    #         print(result_express)
            if not result_express:
                print('error')
                break;

            elif i in [1]:
                result_express=str(result_express).replace('data1','data4')    
            elif i in [3,4,5]:
                result_express=str(result_express).replace('data1','returndata').replace('data2','data4') 

            express+=result_express
        total_task.append(express)
    return total_task


def pv_ma():
    step0=[]  
    for priceitem in price_list:     
        path_dict={}
        path_dict['mode']='gethdfdata'
        path_dict['a']=price_path+priceitem
        step0.append(path_dict) 
    for volitem in vol_list:     
        path_dict={}
        path_dict['mode']='gethdfdata'
        path_dict['a']=vol_path+volitem
        step0.append(path_dict) 
    for ratioitem in ratio_list:     
        path_dict={}
        path_dict['mode']='gethdfdata'
        path_dict['a']=ratio_path+ratioitem
        step0.append(path_dict)

    step1=[]
    for fun1 in ['negselfadivrollingmean']:
        for fun1p1 in [5,10,15,20]:
            path_dict={}
            path_dict['mode']=fun1
            path_dict['a']=fun1p1
            step1.append(path_dict) 

    step2=[]    
    for fun1 in ['tsmin']: #'tsmax','tsmean','decay_linear'
        for fun1p1 in [5,10,15,20]:
            path_dict={}
            path_dict['mode']=fun1
            path_dict['a']=fun1p1
            step2.append(path_dict) 
    total_task=[]
    change_num = list(itertools.product(step0,step1,step2))   
    print('len(change_num)',len(change_num))
    # print(change_num[0])

    for change_item in change_num:
        express=''
        for i in range(len(change_item)):
            result_express = change_factor_signal(change_item[i]) 
    #         print(result_express)
            if not result_express:
                print('error')
                break;
            elif i in [2]:
                result_express=str(result_express).replace('data1','returndata')
            express+=result_express
        total_task.append(express)
    return total_task

def accer():
    step0=[]     
    for pritem in price_list:     
        path_dict={}
        path_dict['mode']='gethdfdata'
        path_dict['a']=price_path+pritem
        step0.append(path_dict) 


    step1=[]
    for fun1 in ['tsselfregression']:
        for fun1p1 in [2,5,10]:
            path_dict={}
            path_dict['mode']=fun1
            path_dict['a']=fun1p1
            step1.append(path_dict)

    step2=[]
    for fun1 in ['negadivbmulN']:
        path_dict={}
        path_dict['mode']=fun1
        step2.append(path_dict) 

    step3=[]    
    for fun1 in ['decay_linear','tsmin','tsmax','tsmean']:
        for fun1p1 in [5,10,15,20]:
            path_dict={}
            path_dict['mode']=fun1
            path_dict['a']=fun1p1
            step3.append(path_dict) 

    total_task=[]
    change_num = list(itertools.product(step0,step1,step2,step3))   
    print('len(change_num)',len(change_num))
    for change_item in change_num:
        express=''
        for i in range(len(change_item)):
            result_express =change_factor_signal(change_item[i]) 
    #         print(result_express)
            if not result_express:
                print('error')
                break;
            elif i in [2,3]:
                result_express=str(result_express).replace('data1','returndata').replace('data2','data1')

            express+=result_express
        total_task.append(express)
    return total_task


def macd():
    step0=[]     
    for pritem in price_list:     
        path_dict={}
        path_dict['mode']='gethdfdata'
        path_dict['a']=price_path+pritem
        step0.append(path_dict) 

    step1=[]
    for fun1 in ['halflifeasubb']:
        for fun1p1 in [(3,9),(5,20),(12,26),(5,10),(10,20),(5,15)]:
            path_dict={}
            path_dict['mode']=fun1
            path_dict['a']=fun1p1
            step1.append(path_dict)

    step2=[]
    # for fun1 in ['tsstd','tsargmax','tsargmin','product','tskurt','tsskew','tsrank','tsselfregression','tsselfcorr','tstma','tszscore']:
    for fun1 in ['asubrollingmean']:
        for fun1p1 in [10]:
            path_dict={}
            path_dict['mode']=fun1
            path_dict['a']=fun1p1
            step2.append(path_dict)

    step3=[]    
    for fun1 in ['decay_linear','tsmin','tsmax','tsmean']:
        for fun1p1 in [5,10,15,20]:
            path_dict={}
            path_dict['mode']=fun1
            path_dict['a']=fun1p1
            step3.append(path_dict) 

    total_task=[]
    change_num = list(itertools.product(step0,step1,step2,step3))   
    print('len(change_num)',len(change_num))
    for change_item in change_num:
        express=''
        for i in range(len(change_item)):
            result_express =change_factor_signal(change_item[i]) 
    #         print(result_express)
            if not result_express:
                print('error')
                break;
            elif i in [2,3]:
                result_express=str(result_express).replace('data1','returndata')

            express+=result_express
        total_task.append(express)
    return total_task
 
def rollvol():
    step0=[]
    for pritem in price_list:     
        path_dict={}
        path_dict['mode']='gethdfdata'
        path_dict['a']=price_path+pritem
        step0.append(path_dict) 

    step1=[]
    for volitem in vol_list:     
        path_dict={}
        path_dict['mode']='gethdfdata'
        path_dict['a']=vol_path+volitem
        step1.append(path_dict) 

    step2=[]
    # for fun1 in ['tsstd','tsargmax','tsargmin','product','tskurt','tsskew','tsrank','tsselfregression','tsselfcorr','tstma','tszscore']:
    for fun1 in ['voladjust']:
        path_dict={}
        path_dict['mode']=fun1
        step2.append(path_dict) 

    step3=[]
    # for fun1 in ['tsstd','tsargmax','tsargmin','product','tskurt','tsskew','tsrank','tsselfregression','tsselfcorr','tstma','tszscore']:
    for fun1 in ['rollingmax','rollingkurt']:
        for fun1p1 in [15]:
            path_dict={}
            path_dict['mode']=fun1
            path_dict['a']=fun1p1
            step3.append(path_dict) 


    step4=[]    
    for fun1 in ['tsmean','tsmin','tsmax']:
        for fun1p1 in [5]:
            path_dict={}
            path_dict['mode']=fun1
            path_dict['a']=fun1p1
            step4.append(path_dict)  


    total_task=[]
    change_num = list(itertools.product(step0,step1,step2,step3,step4))   
    print('len(change_num)',len(change_num))
    # print(change_num[0])

    for change_item in change_num:
        express=''
        for i in range(len(change_item)):
            result_express =change_factor_signal(change_item[i]) 
    #         print(result_express)
            if not result_express:
                print('error')
                break;

            elif i in [1]:
                result_express=str(result_express).replace('data1','data2')    
            elif i in [3,4,5]:
                result_express=str(result_express).replace('data1','returndata')
            express+=result_express
        total_task.append(express)
    return total_task

def lwr():
    step0=[]
    pos=np.random.randint(len(allfile),size=1200).reshape(400,3)
    for i in pos:     
        path_dict={}
        path_dict['mode']='gethdfdata_3'
        path_dict['a']=[allfile[i[0]],allfile[i[1]],allfile[i[2]]]
        step0.append(path_dict) 

    step1=[]
    # for fun1 in ['tsstd','tsargmax','tsargmin','product','tskurt','tsskew','tsrank','tsselfregression','tsselfcorr','tstma','tszscore']:
    for fun1 in ['rsvfunc']:
        for fun1p1 in [5,10]:
            path_dict={}
            path_dict['mode']=fun1
            path_dict['a']=fun1p1
            step1.append(path_dict) 

    step2=[]
    # for fun1 in ['tsstd','tsargmax','tsargmin','product','tskurt','tsskew','tsrank','tsselfregression','tsselfcorr','tstma','tszscore']:
    for fun1 in ['tshalflifeewm']:
        for fun1p1 in [3,6]:
            path_dict={}
            path_dict['mode']=fun1
            path_dict['a']=fun1p1
            step2.append(path_dict)

    step3=[]
    # for fun1 in ['tsstd','tsargmax','tsargmin','product','tskurt','tsskew','tsrank','tsselfregression','tsselfcorr','tstma','tszscore']:
    for fun1 in ['selfsubhalflife']:
        for fun1p1 in [3]:
            path_dict={}
            path_dict['mode']=fun1
            path_dict['a']=fun1p1
            step3.append(path_dict)

    step4=[]    
    for fun1 in ['decay_linear','tsmin','tsmax']:
        for fun1p1 in [5,10]:
            path_dict={}
            path_dict['mode']=fun1
            path_dict['a']=fun1p1
            step4.append(path_dict)  

    total_task=[]
    change_num = list(itertools.product(step0,step1,step2,step3,step4))   
    print('len(change_num)',len(change_num))
    # print(change_num[0])

    for change_item in change_num:
        express=''
        for i in range(len(change_item)):
            result_express =change_factor_signal(change_item[i]) 
    #         print(result_express)
            if not result_express:
                print('error')
                break;

            elif i in [2,3,4]:
                result_express=str(result_express).replace('data1','returndata')    
            express+=result_express
        total_task.append(express)
    return total_task

def bias():
    step0=[]
    for pritem in price_list:     
        path_dict={}
        path_dict['mode']='gethdfdata'
        path_dict['a']=price_path+pritem
        step0.append(path_dict) 
    for volitem in vol_list:     
        path_dict={}
        path_dict['mode']='gethdfdata'
        path_dict['a']=vol_path+volitem
        step0.append(path_dict) 
    for ratioitem in ratio_list:     
        path_dict={}
        path_dict['mode']='gethdfdata'
        path_dict['a']=ratio_path+ratioitem
        step0.append(path_dict) 

    step1=[]
    # for fun1 in ['tsstd','tsargmax','tsargmin','product','tskurt','tsskew','tsrank','tsselfregression','tsselfcorr','tstma','tszscore']:
    for fun1 in ['selfsubdivmean']:
        for fun1p1 in [5,10]:
            path_dict={}
            path_dict['mode']=fun1
            path_dict['a']=fun1p1
            step1.append(path_dict) 

    step2=[]    
    for fun1 in ['decay_linear','tsmin','tsmax']:
        for fun1p1 in [5]:
            path_dict={}
            path_dict['mode']=fun1
            path_dict['a']=fun1p1
            step2.append(path_dict)  

    total_task=[]
    change_num = list(itertools.product(step0,step1,step2))   
    print('len(change_num)',len(change_num))
    # print(change_num[0])

    for change_item in change_num:
        express=''
        for i in range(len(change_item)):
            result_express =change_factor_signal(change_item[i]) 
    #         print(result_express)
            if not result_express:
                print('error')
                break;

            elif i in [2]:
                result_express=str(result_express).replace('data1','returndata')
            express+=result_express
        total_task.append(express)
    return total_task

def emv():
    step0=[]
    for volitem in vol_list:     
        path_dict={}
        path_dict['mode']='gethdfdata'
        path_dict['a']=vol_path+volitem
        step0.append(path_dict) 

    step1=[]
    for priceitem in price_list:     
        path_dict={}
        path_dict['mode']='gethdfdata'
        path_dict['a']=price_path+priceitem
        step1.append(path_dict) 

    step2=[]
    # for fun1 in ['tsstd','tsargmax','tsargmin','product','tskurt','tsskew','tsrank','tsselfregression','tsselfcorr','tstma','tszscore']:
    for fun1 in ['emvfunc']:
        for fun1p1 in [5]:
            path_dict={}
            path_dict['mode']=fun1
            path_dict['a']=fun1p1
            step2.append(path_dict)  



    step3=[]    
    for fun1 in ['decay_linear','tsmin']:
        for fun1p1 in [5]:
            path_dict={}
            path_dict['mode']=fun1
            path_dict['a']=fun1p1
            step3.append(path_dict)  

    total_task=[]
    change_num = list(itertools.product(step0,step1,step2,step3))   
    print('len(change_num)',len(change_num))
    # print(change_num[0])

    for change_item in change_num:
        express=''
        for i in range(len(change_item)):
            result_express =change_factor_signal(change_item[i]) 
    #         print(result_express)
            if not result_express:
                print('error')
                break;

            elif i in [1]:
                result_express=str(result_express).replace('data1','data2')    
            elif i in [3]:
                result_express=str(result_express).replace('data1','returndata')

            express+=result_express
        total_task.append(express)
    return total_task

def boll():
    step0=[]
    for priceitem in price_list:     
        path_dict={}
        path_dict['mode']='gethdfdata'
        path_dict['a']=price_path+priceitem
        step0.append(path_dict) 
    for volitem in vol_list:     
        path_dict={}
        path_dict['mode']='gethdfdata'
        path_dict['a']=vol_path+volitem
        step0.append(path_dict) 
    for ratioitem in ratio_list:     
        path_dict={}
        path_dict['mode']='gethdfdata'
        path_dict['a']=ratio_path+ratioitem
        step0.append(path_dict)

    step1=[]
    # for fun1 in ['tsstd','tsargmax','tsargmin','product','tskurt','tsskew','tsrank','tsselfregression','tsselfcorr','tstma','tszscore']:
    for fun1 in ['tsmean']:
        for fun1p1 in [5,10,20]:
            path_dict={}
            path_dict['mode']=fun1
            path_dict['a']=fun1p1
            step1.append(path_dict) 

    step2=[]
    # for fun1 in ['tsstd','tsargmax','tsargmin','product','tskurt','tsskew','tsrank','tsselfregression','tsselfcorr','tstma','tszscore']:
    for fun1 in ['asubbstdb']:
        for fun1p1 in [5,10,20]:
            path_dict={}
            path_dict['mode']=fun1
            path_dict['a']=fun1p1
            step2.append(path_dict)

    step3=[]
    # for fun1 in ['tsstd','tsargmax','tsargmin','product','tskurt','tsskew','tsrank','tsselfregression','tsselfcorr','tstma','tszscore']:
    for fun1 in ['asubrollingmean']:
        for fun1p1 in [10]:
            path_dict={}
            path_dict['mode']=fun1
            path_dict['a']=fun1p1
            step3.append(path_dict)


    step4=[]    
    for fun1 in ['decay_linear','tsmin','tsmax']:
        for fun1p1 in [5]:
            path_dict={}
            path_dict['mode']=fun1
            path_dict['a']=fun1p1
            step4.append(path_dict)  

    total_task=[]
    change_num = list(itertools.product(step0,step1,step2,step3,step4))   
    print('len(change_num)',len(change_num))
    # print(change_num[0])

    for change_item in change_num:
        express=''
        for i in range(len(change_item)):
            result_express =change_factor_signal(change_item[i]) 
    #         print(result_express)
            if not result_express:
                print('error')
                break;

            elif i in [2]:
                result_express=str(result_express).replace('data2','returndata')    
            elif i in [3,4]:
                result_express=str(result_express).replace('data1','returndata')
            express+=result_express
        total_task.append(express)
    return total_task



def vol_grow():
    step0=[]     
    for volitem in vol_list:     
        path_dict={}
        path_dict['mode']='gethdfdata'
        path_dict['a']=vol_path+volitem
        step0.append(path_dict) 
    step1=[]
    for fun1 in ['growth']:
        for fun1p1 in [1,5,10]:
            path_dict={}
            path_dict['mode']=fun1
            path_dict['a']=fun1p1
            step1.append(path_dict)
    step2=[]
    # for fun1 in ['tsstd','tsargmax','tsargmin','product','tskurt','tsskew','tsrank','tsselfregression','tsselfcorr','tstma','tszscore']:
    for fun1 in ['normalize']:
        for fun1p1 in [5,10,15]:
            path_dict={}
            path_dict['mode']=fun1
            path_dict['a']=fun1p1
            step2.append(path_dict)
    step3=[]    
    for fun1 in ['decay_linear','tsmin','tsmax','tsmean']:
        for fun1p1 in [5,10,15,20]:
            path_dict={}
            path_dict['mode']=fun1
            path_dict['a']=fun1p1
            step3.append(path_dict) 
    total_task=[]
    change_num = list(itertools.product(step0,step1,step2,step3))   
    print('len(change_num)',len(change_num))
    for change_item in change_num:
        express=''
        for i in range(len(change_item)):
            result_express =change_factor_signal(change_item[i]) 
    #         print(result_express)
            if not result_express:
                print('error')
                break;
            elif i in [2,3]:
                result_express=str(result_express).replace('data1','returndata')
            express+=result_express
        total_task.append(express)
    return total_task



def rankcorr():
    step0=[]     
    for volitem in vol_list:     
        path_dict={}
        path_dict['mode']='gethdfdata'
        path_dict['a']=vol_path+volitem
        step0.append(path_dict) 
    step1=[]
    for pritem in price_list:     
        path_dict={}
        path_dict['mode']='gethdfdata'
        path_dict['a']=price_path+pritem
        step1.append(path_dict) 
    step2=[]
    for fun1 in ['rankcorr']:
        for fun1p1 in [3]:
            path_dict={}
            path_dict['mode']=fun1
            path_dict['a']=fun1p1
            step2.append(path_dict) 
    step3=[]    
    for fun1 in ['decay_linear','tsmin']:
        for fun1p1 in [5]:
            path_dict={}
            path_dict['mode']=fun1
            path_dict['a']=fun1p1
            step3.append(path_dict) 
    total_task=[]
    change_num = list(itertools.product(step0,step1,step2,step3))   
    print('len(change_num)',len(change_num))
    for change_item in change_num:
        express=''
        for i in range(len(change_item)):
            result_express =change_factor_signal(change_item[i]) 
            if not result_express:
                print('error')
                break;
            elif i in [1]:
                result_express=str(result_express).replace('data1','data2')
            elif i in [3]:
                result_express=str(result_express).replace('data1','returndata')
            express+=result_express
        total_task.append(express)
    return total_task

def pri_rank():
    step0=[]     
    pos=np.random.randint(len(price_list),size=1200).reshape(400,3)
    for i in pos:     
        path_dict={}
        path_dict['mode']='gethdfdata_3'
        path_dict['a']=[price_path+price_list[i[0]],price_path+price_list[i[1]],price_path+price_list[i[2]]]
        step0.append(path_dict) 

    step1=[]
    for fun1 in ['rankasubbmean']:
        for fun1p1 in [3,5,15]:
            path_dict={}
            path_dict['mode']=fun1
            path_dict['a']=fun1p1
            step1.append(path_dict)


    step2=[]
    for fun1 in ['rankasubb']:
        path_dict={}
        path_dict['mode']=fun1
        step2.append(path_dict) 

    step3=[]    
    for fun1 in ['amulbmean']:
        for fun1p1 in [5,10]:
            path_dict={}
            path_dict['mode']=fun1
            path_dict['a']=fun1p1
            step3.append(path_dict) 

    step4=[]    
    for fun1 in ['decay_linear','tsmin','tsmax']:
        for fun1p1 in [5,10,15]:
            path_dict={}
            path_dict['mode']=fun1
            path_dict['a']=fun1p1
            step4.append(path_dict) 

    total_task=[]
    change_num = list(itertools.product(step0,step1,step2,step3,step4))   
    print('len(change_num)',len(change_num))
    for change_item in change_num:
        express=''
        for i in range(len(change_item)):
            result_express =change_factor_signal(change_item[i]) 
            if not result_express:
                print('error')
                break;
            elif i in [1]:
                result_express=str(result_express).replace('returndata','data1')
            elif i in [2]:
                result_express=str(result_express).replace('returndata','data2').replace('data1','data3')
            elif i in [4]:
                result_express=str(result_express).replace('data1','returndata')
            express+=result_express
        total_task.append(express)
    return total_task

def elasticity():
    step0=[]     
    for priceitem in price_list:     
        path_dict={}
        path_dict['mode']='gethdfdata'
        path_dict['a']=price_path+priceitem
        step0.append(path_dict) 
    step1=[]
    for volitem in vol_list:     
        path_dict={}
        path_dict['mode']='gethdfdata'
        path_dict['a']=vol_path+volitem
        step1.append(path_dict) 
    step2=[]
    for fun1 in ['growthrate']:
        for fun1p1 in [1]:
            path_dict={}
            path_dict['mode']=fun1
            path_dict['a']=fun1p1
            step2.append(path_dict) 
    step3=[]
    for fun1 in ['growthrate']:
        for fun1p1 in [1]:
            path_dict={}
            path_dict['mode']=fun1
            path_dict['a']=fun1p1
            step3.append(path_dict) 
    step4=[]
    for fun1 in ['adivbmean']:
        for fun1p1 in [5]:
            path_dict={}
            path_dict['mode']=fun1
            path_dict['a']=fun1p1
            step4.append(path_dict)        
    step5=[]    
    for fun1 in ['decay_linear','tsmin','tsmax']:
        for fun1p1 in [5]:
            path_dict={}
            path_dict['mode']=fun1
            path_dict['a']=fun1p1
            step5.append(path_dict) 
    total_task=[]
    change_num = list(itertools.product(step0,step1, step2,step3,step4,step5))   
    print('len(change_num)',len(change_num))

    for change_item in change_num:
        express=''
        for i in range(len(change_item)):
            result_express =change_factor_signal(change_item[i]) 
    #         print(result_express)
            if not result_express:
                print('error')
                break;

            elif i in [1]:
                result_express=str(result_express).replace('data1','data2')
            elif i in [2]:
                result_express=str(result_express).replace('returndata','data1')
            elif i in [3]:
                result_express=str(result_express).replace('returndata','data2').replace('data1','data2')     
            elif i in [5]:
                result_express=str(result_express).replace('data1','returndata')        
            express+=result_express
        total_task.append(express)
    return total_task

def BASIC_DAY_PRICE():
    step0=[]     
    for priceitem in daily_price_list:     
        path_dict={}
        path_dict['mode']='daily'
        path_dict['a']=priceitem
        step0.append(path_dict) 

    step1=[]
    for priceitem in daily_price_list:     
        path_dict={}
        path_dict['mode']='daily'
        path_dict['a']=priceitem
        step1.append(path_dict) 

    step2=[]
    for fun1 in ['asubb']:
        path_dict={}
        path_dict['mode']=fun1
        step2.append(path_dict) 

    total_task=[]
    change_num = list(itertools.product(step0,step1, step2))   
    print('len(change_num)',len(change_num))

    for change_item in change_num:
        express=''
        for i in range(len(change_item)):
            result_express =change_factor_signal(change_item[i]) 
    #         print(result_express)
            if not result_express:
                print('error')
                break;min_keep

            elif i in [1]:
                result_express=str(result_express).replace('data1','data2')

            express+=result_express
        total_task.append(express)
    return total_task

def BASIC_DAY(factor_type):
    # if factor_type == 'all':
    #     return BASIC_DAY('price')+BASIC_DAY('vol')+BASIC_DAY('ratio')
    step0=[
    {'type':'price','factor':'tclose','express':'data1=tclose'},
    {'type':'price','factor':'topen','express':'data1=topen'},
    {'type':'price','factor':'highday','express':'data1=highday'},
    {'type':'price','factor':'lowday','express':'data1=lowday'},
    {'type':'price','factor':'hl','express':'data1=0.5*(highday+lowday)'},
    {'type':'price','factor':'cmv','express':'data1=tclose-vwap'},
    {'type':'price','factor':'cml','express':'data1=tclose - lowday'},
    {'type':'price','factor':'cmo','express':'data1=tclose - topen'},
    {'type':'price','factor':'cmh','express':'data1=tclose - highday'},
    {'type':'price','factor':'vml','express':'data1=vwap - lowday'},   
    {'type':'price','factor':'cmhl','express':'data1=tclose - (highday+lowday)/2'},
    {'type':'price','factor':'hml','express':'data1=highday-lowday'},
    {'type':'price','factor':'cmo_hml','express':'data1=(tclose -topen) + (highday-lowday)'},    
    {'type':'price','factor':'cohl','express':'data1=0.25*(highday+topen+lowday+tclose)'},
    {'type':'price','factor':'chl','express':'data1=(tclose+highday+lowday)/3.'},  
    {'type':'price','factor':'vwap','express':'data1=vwap'},    
    {'type':'price','factor':'omhl','express':'data1=topen - (highday+lowday)/2'},
    {'type':'price','factor':'tstcmeandelta','express':'data1=alphalib.ts_mean(tclose,5)-alphalib.ts_mean(tclose.shift(5),5)'},
    {'type':'price','factor':'tsvwmeandelta','express':'data1=alphalib.ts_mean(vwap,5)-alphalib.ts_mean(vwap.shift(5),5)'},
    {'type':'price','factor':'tshmeandelta','express':'data1=alphalib.ts_mean(highday,5)-alphalib.ts_mean(highday.shift(5),5)'},
    {'type':'price','factor':'tslmeandelta','express':'data1=alphalib.ts_mean(lowday,5)-alphalib.ts_mean(lowday.shift(5),5)'},
    {'type':'price','factor':'tstomeandelta','express':'data1=alphalib.ts_mean(topen,5)-alphalib.ts_mean(topen.shift(5),5)'},
    {'type':'price','factor':'tcsubmean','express':'data1=tclose-alphalib.ts_mean(tclose,5)'},   
    {'type':'price','factor':'vwapsubmean','express':'data1=vwap-alphalib.ts_mean(vwap,5)'},   
    
    {'type':'vol','factor':'volume','express':'data1=volume'},
    {'type':'vol','factor':'amount','express':'data1=amount'},
    {'type':'vol','factor':'voldtsum5','express':'data1=volume/alphalib.ts_sum(volume,5).replace(0,np.nan)'},      
    {'type':'vol','factor':'volrollmaxsub','express':'data1=volume.rolling(5).max()-volume.rolling(15).max()'},      
    {'type':'vol','factor':'volrollmeansub','express':'data1=volume.rolling(5).mean()-volume.rolling(15).mean()'},      
    {'type':'vol','factor':'amtrollmaxsub','express':'data1=amount.rolling(5).max()-amount.rolling(15).max()'},      
    {'type':'vol','factor':'amtrollmeansub','express':'data1=amount.rolling(5).mean()-amount.rolling(15).mean()'},   
    {'type':'vol','factor':'tsvolsumdelta','express':'data1=alphalib.ts_sum(volume,5)-alphalib.ts_sum(volume.shift(5),5)'},
    {'type':'vol','factor':'tsamtsumdelta','express':'data1=alphalib.ts_sum(amount,5)-alphalib.ts_sum(amount.shift(5),5)'},
    {'type':'vol','factor':'volsubmean','express':'data1=volume-alphalib.ts_mean(volume,5)'},   
    {'type':'vol','factor':'amtsubmean','express':'data1=amount-alphalib.ts_mean(amount,5)'},   
    {'type':'vol','factor':'vmulc','express':'data1=volume*tclose'},
        
    {'type':'ratio','factor':'volret','express':'data1=volume/volume.shift(1).replace(0,np.nan)-1'}, 
    {'type':'ratio','factor':'amtret','express':'data1=amount/amount.shift(1).replace(0,np.nan)-1'},    
    {'type':'ratio','factor':'tcstd','express':'data1=tclose.rolling(5).std()'},    
    {'type':'ratio','factor':'tostd','express':'data1=topen.rolling(5).std()'},    
    {'type':'ratio','factor':'vwapstd','express':'data1=vwap.rolling(5).std()'},    
    {'type':'ratio','factor':'volstd','express':'data1=volume.rolling(5).std()'},    
    {'type':'ratio','factor':'amtstd','express':'data1=amount.rolling(5).std()'},    
    {'type':'ratio','factor':'retstd','express':'data1=rets.rolling(5).std()'},
    {'type':'ratio','factor':'rets','express':'data1=rets'},
    {'type':'ratio','factor':'whtrets','express':'data1=rets*volume/alphalib.ts_sum(volume,5).replace(0,np.nan)'},
    {'type':'ratio','factor':'turnover','express':'data1=turnover'},
    {'type':'ratio','factor':'cdv','express':'data1=tclose/vwap'},
    {'type':'ratio','factor':'inret','express':'data1=(tclose/topen - 1.).replace([np.inf,-np.inf],np.nan)'},
    {'type':'ratio','factor':'outret','express':'data1=-1*(topen/tclose.shift(1)-1.).replace([np.inf,-np.inf],np.nan)'},
    {'type':'ratio','factor':'volsignret','express':'data1=alphalib.sign(delta(volume,1))*rets'},
    {'type':'ratio','factor':'volret','express':'data1=volume*rets'},
    {'type':'ratio','factor':'scaledret','express':'data1=alphalib.z_score(rets)'},
    {'type':'ratio','factor':'tsvolsumdelta','express':'data1=alphalib.ts_sum(volume,5)/alphalib.ts_sum(volume.shift(5),5).replace(0,np.nan)-1'},

        # ts_self_regression 10 价格
        # ts_self_corr vol
    ]
    step1=[]    
    for fun1 in ['keep','tsmean','decay_linear','tsmin','tsmax','delta','tsrank','tsargmax','tsargmin','tszscore','tsselfregression','tsselfcorr']:
        for fun1p1 in [5]:
            path_dict={}
            path_dict['mode']=fun1
            path_dict['a']=fun1p1
            if fun1 != 'keep' or fun1p1 ==5:
                step1.append(path_dict)      

    total_task={'price':[],'vol':[],'ratio':[]}
    for item in step0:
        for step1_item in step1: 
            if (step1_item['mode']=='tsselfcorr' and item['type'] in ['price','ratio']) or (step1_item['mode']=='tsselfregression' and item['type'] in ['vol','ratio']):
                continue
            total_item={}
            express=item['express']
            express+= str(change_factor_signal(step1_item))
            express=replace_source_name(express)
            total_item['express']=express
            name=item['factor']
            total_item['factor_name']=name if step1_item['mode']=='keep' else name+'_'+step1_item['mode']+'_'+str(step1_item['a'])
            total_task[item['type']].append(total_item)
    print('len(total_task)',len(total_task[factor_type]))
    return total_task[factor_type]

def BASIC_MIN(factor_type):
    # if factor_type == 'all':
    #     return BASIC_MIN('price')+BASIC_MIN('vol')+BASIC_MIN('ratio')

    step0=[
    {'type': 'price', 'factor': 'high', 'express': 'data1=highmin'},
    {'type': 'price', 'factor': 'open', 'express': 'data1=openmin'},
    {'type': 'price', 'factor': 'low', 'express': 'data1=lowmin'},
    {'type': 'price', 'factor': 'close', 'express': 'data1=closemin'},
    {'type': 'price', 'factor': 'vwap', 'express': 'data1=(amtmin/volmin).replace([np.inf,-np.inf],np.nan)'},
    {'type': 'price', 'factor': 'hl', 'express': 'data1=(highmin + lowmin)/2'},
    {'type': 'price', 'factor': 'cmv', 'express': 'data1=closemin -(amtmin/volmin).replace([np.inf,-np.inf],np.nan)'},
    {'type': 'price', 'factor': 'cml', 'express': 'data1=closemin -lowmin'},
    {'type': 'price', 'factor': 'cmo', 'express': 'data1=closemin -openmin'},
    {'type': 'price', 'factor': 'cmh', 'express': 'data1=closemin -highmin'},
    {'type': 'price', 'factor': 'cmhl', 'express': 'data1=closemin - (highmin + lowmin)/2'},
    {'type': 'price', 'factor': 'hml', 'express': 'data1=highmin - lowmin'},
    {'type': 'price', 'factor': 'cmo_hml', 'express': 'data1=closemin - openmin + highmin - lowmin'},
    {'type': 'price', 'factor': 'cohl', 'express': 'data1=0.25*(highmin+openmin+lowmin+closemin)'},
    {'type': 'price', 'factor': 'chl', 'express': 'data1=(closemin+highmin+lowmin)/3.'} ,   
    {'type': 'price', 'factor': 'cmhl', 'express': 'data1=closemin - (highmin + lowmin)/2'},
    {'type':'price','factor':'omhl','express':'data1=openmin - (highmin+lowmin)/2'},
    {'type':'price','factor':'tstcmeandelta','express':'data1=alphalib.ts_mean(closemin,5)-alphalib.ts_mean(closemin.shift(5),5)'},
    {'type':'price','factor':'tsvwmeandelta','express':'data1=alphalib.ts_mean((amtmin/volmin).replace([np.inf,-np.inf],np.nan),5)-alphalib.ts_mean((amtmin/volmin).replace([np.inf,-np.inf],np.nan).shift(5),5)'},
    {'type':'price','factor':'tshmeandelta','express':'data1=alphalib.ts_mean(highmin,5)-alphalib.ts_mean(highmin.shift(5),5)'},
    {'type':'price','factor':'tslmeandelta','express':'data1=alphalib.ts_mean(lowmin,5)-alphalib.ts_mean(lowmin.shift(5),5)'},
    {'type':'price','factor':'tstomeandelta','express':'data1=alphalib.ts_mean(openmin,5)-alphalib.ts_mean(openmin.shift(5),5)'},
    {'type':'price','factor':'tcsubmean','express':'data1=closemin-alphalib.ts_mean(closemin,5)'},   
    {'type':'price','factor':'vwapsubmean','express':'data1=(amtmin/volmin).replace([np.inf,-np.inf],np.nan)-alphalib.ts_mean((amtmin/volmin).replace([np.inf,-np.inf],np.nan),5)'},      
        
    {'type': 'vol', 'factor': 'tradecount', 'express': 'data1=tradecountmin'},
    {'type': 'vol', 'factor': 'orderamt', 'express': 'data1=(amtmin/tradecountmin).replace([np.inf,-np.inf],np.nan)'},
   
    {'type': 'vol', 'factor': 'amt', 'express': 'data1=amtmin'},
    {'type': 'vol', 'factor': 'vol', 'express': 'data1=volmin'},
    {'type': 'vol', 'factor': 'buyamt', 'express': 'data1=buyamtmin'},
    {'type': 'vol', 'factor': 'buycount', 'express': 'data1=buycountmin'},
    {'type': 'vol', 'factor': 'buyvol', 'express': 'data1=buyvolmin'},
    {'type': 'vol', 'factor': 'saleamt', 'express': 'data1=saleamtmin'},
    {'type': 'vol', 'factor': 'salevol', 'express': 'data1=salevolmin'},
    {'type': 'vol', 'factor': 'salecount', 'express': 'data1=salecountmin'},
    {'type': 'vol', 'factor': 'bmscount', 'express': 'data1=buycountmin - salecountmin'},
    {'type': 'vol', 'factor': 'bmsvol', 'express': 'data1=buyvolmin - salevolmin'},
        
    {'type':'vol','factor':'mvolrollmaxsub','express':'data1=volmin.rolling(5).max()-volmin.rolling(15).max()'},      
    {'type':'vol','factor':'mamtrollmaxsub','express':'data1=amtmin.rolling(5).max()-amtmin.rolling(15).max()'},      
    {'type':'vol','factor':'mbamtrollmaxsub','express':'data1=buyamtmin.rolling(5).max()-buyamtmin.rolling(15).max()'},      
    {'type':'vol','factor':'mbcourollmaxsub','express':'data1=buycountmin.rolling(5).max()-buycountmin.rolling(15).max()'},      
    {'type':'vol','factor':'mbvolrollmaxsub','express':'data1=buyvolmin.rolling(5).max()-buyvolmin.rolling(15).max()'},      
    {'type':'vol','factor':'msamtrollmaxsub','express':'data1=saleamtmin.rolling(5).max()-saleamtmin.rolling(15).max()'},      
    {'type':'vol','factor':'mscourollmaxsub','express':'data1=salecountmin.rolling(5).max()-salecountmin.rolling(15).max()'},      
    {'type':'vol','factor':'msvolrollmaxsub','express':'data1=salevolmin.rolling(5).max()-salevolmin.rolling(15).max()'},      

    {'type':'vol','factor':'mvolrollmeansub','express':'data1=volmin.rolling(5).mean()-volmin.rolling(15).mean()'},      
    {'type':'vol','factor':'mamtrollmeansub','express':'data1=amtmin.rolling(5).mean()-amtmin.rolling(15).mean()'},      
    {'type':'vol','factor':'mbamtrollmeansub','express':'data1=buyamtmin.rolling(5).mean()-buyamtmin.rolling(15).mean()'},      
    {'type':'vol','factor':'mbcourollmeansub','express':'data1=buycountmin.rolling(5).mean()-buycountmin.rolling(15).mean()'},      
    {'type':'vol','factor':'mbvolrollmeansub','express':'data1=buyvolmin.rolling(5).mean()-buyvolmin.rolling(15).mean()'},      
    {'type':'vol','factor':'msamtrollmeansub','express':'data1=saleamtmin.rolling(5).mean()-saleamtmin.rolling(15).mean()'},      
    {'type':'vol','factor':'mscourollmeansub','express':'data1=salecountmin.rolling(5).mean()-salecountmin.rolling(15).mean()'},      
    {'type':'vol','factor':'msvolrollmeansub','express':'data1=salevolmin.rolling(5).mean()-salevolmin.rolling(15).mean()'},  

    {'type':'vol','factor':'mvolsumdelta','express':'data1=alphalib.ts_sum(volmin,5)-alphalib.ts_sum(volmin.shift(5),5)'},
    {'type':'vol','factor':'mamtsumdelta','express':'data1=alphalib.ts_sum(amtmin,5)-alphalib.ts_sum(amtmin.shift(5),5)'},
    {'type':'vol','factor':'mbamtsumdelta','express':'data1=alphalib.ts_sum(buyamtmin,5)-alphalib.ts_sum(buyamtmin.shift(5),5)'},
    {'type':'vol','factor':'mbcousumdelta','express':'data1=alphalib.ts_sum(buycountmin,5)-alphalib.ts_sum(buycountmin.shift(5),5)'},
    {'type':'vol','factor':'mbvolsumdelta','express':'data1=alphalib.ts_sum(buyvolmin,5)-alphalib.ts_sum(buyvolmin.shift(5),5)'},
    {'type':'vol','factor':'msamtsumdelta','express':'data1=alphalib.ts_sum(saleamtmin,5)-alphalib.ts_sum(saleamtmin.shift(5),5)'},
    {'type':'vol','factor':'mscousumdelta','express':'data1=alphalib.ts_sum(salecountmin,5)-alphalib.ts_sum(salecountmin.shift(5),5)'},
    {'type':'vol','factor':'msvolsumdelta','express':'data1=alphalib.ts_sum(salevolmin,5)-alphalib.ts_sum(salevolmin.shift(5),5)'},

    {'type':'vol','factor':'mvolsubmean','express':'data1=volmin-alphalib.ts_mean(volmin,5)'},  
    {'type':'vol','factor':'mamtsubmean','express':'data1=amtmin-alphalib.ts_mean(amtmin,5)'},   
    {'type':'vol','factor':'mbamtsubmean','express':'data1=buyamtmin-alphalib.ts_mean(buyamtmin,5)'},   
    {'type':'vol','factor':'mbcousubmean','express':'data1=buycountmin-alphalib.ts_mean(buycountmin,5)'},   
    {'type':'vol','factor':'mbvolsubmean','express':'data1=buyvolmin-alphalib.ts_mean(buyvolmin,5)'},   
    {'type':'vol','factor':'msamtsubmean','express':'data1=saleamtmin-alphalib.ts_mean(saleamtmin,5)'},   
    {'type':'vol','factor':'mscousubmean','express':'data1=salecountmin-alphalib.ts_mean(salecountmin,5)'},   
    {'type':'vol','factor':'msvolsubmean','express':'data1=salevolmin-alphalib.ts_mean(salevolmin,5)'},  
        
    {'type': 'ratio', 'factor': 'rets', 'express': 'data1=np.log(closemin.replace(0,np.nan)) - np.log(closemin.shift(1).replace(0,np.nan))'},
    {'type': 'ratio', 'factor': 'cdv', 'express': 'data1=closemin/(amtmin/volmin).replace([np.inf,-np.inf],np.nan)'},
    {'type': 'ratio', 'factor': 'intret', 'express': 'data1=(closemin/openmin - 1.).replace([np.inf,-np.inf],np.nan)'},
    {'type': 'ratio', 'factor': 'outret', 'express': 'data1=-1*(openmin/closemin.shift(1)-1.).replace([np.inf,-np.inf],np.nan)'},
    {'type': 'ratio', 'factor': 'volsignret', 'express': 'data1=alphalib.sign(alphalib.delta(volmin,1))*np.log(closemin.replace(0,np.nan)) - np.log(closemin.shift(1).replace(0,np.nan))'},
    {'type': 'ratio', 'factor': 'volret', 'express': 'data1=volmin*np.log(closemin.replace(0,np.nan)) - np.log(closemin.shift(1).replace(0,np.nan))'},
    {'type': 'ratio', 'factor': 'scaledret', 'express': 'data1=alphalib.z_score(np.log(closemin.replace(0,np.nan)) - np.log(closemin.shift(1).replace(0,np.nan)))'}
    
    ]
    step1=[]    
    for fun1 in ['keep','tsmean','decay_linear','tsmin','tsmax','delta','tsrank','tsargmax','tsargmin','tszscore']:
        for fun1p1 in [8,32]:
            path_dict={}
            path_dict['mode']=fun1
            path_dict['a']=fun1p1
            if fun1 != 'keep':
                step1.append(path_dict)         
    step2=[]    
    for fun1 in ['changeDayData']:
        path_dict={}
        path_dict['mode']=fun1
        step2.append(path_dict)  
        
    total_task={'price':[],'vol':[],'ratio':[]}
    for item in step0:
        for step1_item in step1: 
            for step2_item in step2:
                total_item={}
                express=item['express']
                express+= str(change_factor_signal(step1_item))
                express+= str(change_factor_signal(step2_item)).replace('data1','returndata')
                express=replace_source_name(express)
                total_item['express']=express
                name='min30_'+item['factor']
                total_item['factor_name']=name if step1_item['mode']=='keep' else name+'_'+step1_item['mode']+'_'+str(step1_item['a'])
                total_task[item['type']].append(total_item)
    print('len(total_task)',len(total_task[factor_type]))
    return total_task[factor_type]

def BASIC_TICK(factor_type):
    # if factor_type == 'all':
    #     return BASIC_TICK('price')+BASIC_TICK('vol')+BASIC_TICK('ratio')
    step0=[
    {'type':'ratio','factor':'priceratio','express':'data1 = priceratio'},
    {'type':'ratio','factor':'sizeratio','express':'data1 = sizeratio'},
    {'type':'price','factor':'askprice1','express':'data1 = askprice1'},
    {'type':'price','factor':'ask_mean','express':'data1 = (askprice1 + askprice2 + askprice3 + askprice4 + askprice5)/5.'},
    {'type':'price','factor':'bid_mean','express':'data1 = (bidprice1 + bidprice2 + bidprice3 + bidprice4 + bidprice5)/5.'},
    {'type':'price','factor':'bidprice1','express':'data1 = bidprice1'},
    {'type':'vol','factor':'asksize1','express':'data1 = asksize1'},
    {'type':'vol','factor':'bidsize1','express':'data1 = bidsize1'},
    {'type':'price','factor':'asksize_mean','express':'data1 = (asksize1 + asksize2 + asksize3 + asksize4 + asksize5)/5.'},
    {'type':'price','factor':'bidsize_mean','express':'data1 = (bidsize1 + bidsize2 + bidsize3 + bidsize4 + bidsize5)/5.'},
    {'type':'price','factor':'bookprice1','express':'data1 = bookprice1'},
    {'type':'price','factor':'bookprice_mean','express':'data1 = (bookprice1 + bookprice2 + bookprice3 + bookprice4 + bookprice5)/5.'},
    {'type':'vol','factor':'askorderflow','express':'data1 = askorderflow'},
    {'type':'vol','factor':'bidorderflow','express':'data1 = bidorderflow'},
    {'type':'price','factor':'minprice','express':'data1 = (askprice1+bidprice1)/2.'},
    {'type':'price','factor':'ask_vwap','express':'data1 = ask_vwap'},
    {'type':'price','factor':'bid_vwap','express':'data1 = bid_vwap'},
    {'type':'price','factor':'min_vwap','express':'data1 = (ask_vwap + bid_vwap)/2.'}
    ]
    step1=[]    
    for fun1 in ['keep','tsmean','decay_linear','tsmin','tsmax','delta','tsrank','tsargmax','tsargmin','tszscore']:
        for fun1p1 in [8,32]:
            path_dict={}
            path_dict['mode']=fun1
            path_dict['a']=fun1p1
            if fun1 != 'keep':
                step1.append(path_dict)      
    step2=[]    
    for fun1 in ['changeDayData']:
        path_dict={}
        path_dict['mode']=fun1
        step2.append(path_dict)  
    total_task={'price':[],'vol':[],'ratio':[]}
    for item in step0:
        for step1_item in step1: 
            for step2_item in step2:
                total_item={}
                express=item['express']
                express+= str(change_factor_signal(step1_item))
                express+= str(change_factor_signal(step2_item)).replace('data1','returndata')
                express=replace_source_name(express)
                total_item['express']=express
                name=item['factor']
                total_item['factor_name']=name if step1_item['mode']=='keep' else name+'_'+step1_item['mode']+'_'+str(step1_item['a'])
                total_task[item['type']].append(total_item)
    print('len(total_task)',len(total_task[factor_type]))
    return total_task[factor_type]

def make_totalexpress_by_model_name(modelname,factor_type=''):
    if modelname=='tclosedivvwap':
        express = tclosedivvwap()
    elif modelname =='pv_vpt':
        express = pv_vpt()
    elif modelname =='pv_cho': 
        express = pv_cho()
    elif modelname=='pv_ma':
        express = pv_ma()
    elif modelname=='accer':
        express = accer()             
    elif modelname =='macd':
        express = macd()  
    elif modelname =='rollvol':
        express = rollvol()  
    elif modelname=='lwr':
        express = lwr()   
    elif modelname=='bias':
        express = bias()
    elif modelname =='emv': 
        express = emv()
    elif modelname=='boll':
        express = boll()
    elif modelname=='vol_grow':
        express = vol_grow()   
    elif modelname=='rank_corr':
        express = rankcorr()   
    elif modelname=='pri_rank':
        express = pri_rank()     
    elif modelname=='elasticity':
        express = elasticity() 
    elif modelname=='BASIC_DAY_PRICE':
        express = BASIC_DAY_PRICE() 
    elif modelname=='BASIC_DAY':
        express = BASIC_DAY(factor_type) 
    elif modelname=='BASIC_MIN':
        express = BASIC_MIN(factor_type) 
    elif modelname=='BASIC_TICK':
        express = BASIC_TICK(factor_type) 
    else:
        return False
        
    return express


